This little helper function writes results
to an excel file. If the file already exists and the overwrite
flag is set, the file should be overwritten if possible - that is when the file is not opened by any other application (Excel in this case):
def writeXls(results, fname, overwrite=False):
if os.path.isfile(fname) and not overwrite:
print("Error: can't write to {} because it already exists and overwrite flag is false".format(fname))
return
# doesn't work as intended but write access should be checked first. Fix this.
if not os.access(fname, os.W_OK):
print("Error: can't write to {} because write access is not allowed. It might be already opened by other software.".format(fname))
The problem is that the test for W_OK passes, even if the file is opened in Excel:
f = open(file_name_or_filelike_obj, 'w+b')
IOError: [Errno 13] Permission denied: <fname here>
- Why is that so
- and how should I test for write access to the file instead?