permission denied issue when trying to access file

2019-07-19 18:01发布

问题:

Edit: I deleted this but I'm going to undelete because I think it could be useful. And post what was actually happening which I didn't realize at the time.

Original Question: I'm trying to open a set of excel files where one is currently open by another user. All of the other files work, but this one I get 'permission denied' errors.

Windows gives you the option to "read only" open the file, but I can't seem to find an equivalent in python (xlrd), so I thought I would copy the file to a temp location and open it; but that doesn't work either. In either case, i get:

IOError: [Errno 13] Permission denied:

Is it possible to either:

  • open an excel file in read only mode (like windows) in xlrd
  • copy a file currently in use by another user

thanks !

回答1:

As it turns out, I the files that were failing were also open by other users, so when I went to access them through windows explorer it seemed natural another user having the file open would cause the permission denied error; however the real issue is that as I was looping through the folder I was attempting to access the temp files created by Excel for the other user who was accessing the file.

So, for a given file "old_file.xlsx" that a user had open, their instance of excel created "~$old_file.xlsx"

How I got around this was:

files_to_check = [f for f in os.listdir(PATH) if os.path.isfile(f)]
files_to_check = [f for f in files_to_check if '~' not in f and 'xlsx' in f]

basically, just making sure that they're the non-temp, xlsx files that I planned to go through.