尝试使用 xlrd 或 shutil 访问文件夹中的文件时出现权限被拒绝问题 [英] permission denied issue when trying to access files in a folder with xlrd or shutil
问题描述
我删除了它,但我将取消删除,因为我认为它可能有用.并发布我当时没有意识到的实际发生的事情.
原始问题:我正在尝试打开一组 excel 文件,其中一个文件当前由另一个用户打开.所有其他文件都有效,但我收到权限被拒绝"错误.
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 为您提供了只读"打开文件的选项,但我似乎无法在 python (xlrd) 中找到等效项,因此我想我会将文件复制到临时位置并打开它;但这也不起作用.无论哪种情况,我都会得到:
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:
是否可以:
- 在 xlrd 中以只读模式(如 windows)打开一个 excel 文件
- 复制另一个用户当前正在使用的文件
谢谢!
推荐答案
事实证明,我失败的文件也被其他用户打开,所以当我通过 Windows 资源管理器访问它们时,另一个用户似乎很自然打开文件会导致权限被拒绝错误;然而,真正的问题是,当我在文件夹中循环时,我试图访问 Excel 为访问该文件的其他用户创建的临时文件.
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.
因此,对于用户打开的给定文件old_file.xlsx",他们的 excel 实例创建了~$old_file.xlsx"
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]
基本上,只要确保它们是我计划通过的非临时 xlsx 文件.
basically, just making sure that they're the non-temp, xlsx files that I planned to go through.
这篇关于尝试使用 xlrd 或 shutil 访问文件夹中的文件时出现权限被拒绝问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!