permission denied issue when trying to access files in a folder with xlrd or shutil

瘦欲@ 提交于 2020-01-24 13:35:19

问题


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.



来源:https://stackoverflow.com/questions/36201933/permission-denied-issue-when-trying-to-access-files-in-a-folder-with-xlrd-or-shu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!