即使明确设置权限,也无法在Python中删除Windows文件

发布于 2025-02-05 18:35:39 字数 1624 浏览 2 评论 0原文

我有一个代码,将存档文件解压缩到Windows的温度目录中。我存储了这个没有拉链的临时位置,并且完成任务后,我尝试将其删除。

Class Check():
 def __init__(self, file_path, unzip_dir=None):
   self.file_path = file_path
   self.unzip_dir = unzip_dir


 def terminate(self):
   if self.unzip_dir is not None:
      unzipped_resources = os.path.join(self.unzip_dir, 'resources')
      os.chmod(unzipped_resources, stat.S_IWRITE |  stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
      shutil.rmtree(self.unzip_dir, ignore_errors=False) 

我不知道为什么会引发此错误:

Traceback (most recent call last):
  File "C:\fakepath\Test\test.py", line 94, in tearDownClass
    CheckFile.obj.terminate()
  File "C:\fakepath\Test\test.py", line 58, in terminate
    shutil.rmtree(self.unzip_dir, ignore_errors=False)
  File "C:\Program Files\Python39\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Program Files\Python39\lib\shutil.py", line 613, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Program Files\Python39\lib\shutil.py", line 618, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Program Files\Python39\lib\shutil.py", line 616, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\USERNAME\\AppData\\Local\\Temp\\tmplphs09bi\\resources\\testfile.dll'

我仔细检查了使用testfile.dll在使用后释放DLL的过程。我什至可以通过转到该位置手动删除文件 - 这样告诉我它不再被任何人使用。我已经尝试明确设置文件的权限和父文件夹。有什么可能导致这一点,如何解决?

可能值得注意的是,该类对象在Python的一开始和结束时正在创建和破坏。

编辑:我发现即使在整棵树上明确并递归地调用os.chmod,也没有设置权限。致电Chmod之前和之后的许可保持不变,如果有人帮助确定原因,将感到高兴。

I have a code which unzips an archived file into the temp directory of Windows. I store this unzipped temporary location and upon completion of my task, I try to delete it.

Class Check():
 def __init__(self, file_path, unzip_dir=None):
   self.file_path = file_path
   self.unzip_dir = unzip_dir


 def terminate(self):
   if self.unzip_dir is not None:
      unzipped_resources = os.path.join(self.unzip_dir, 'resources')
      os.chmod(unzipped_resources, stat.S_IWRITE |  stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
      shutil.rmtree(self.unzip_dir, ignore_errors=False) 

I have no clue why it is throwing this error:

Traceback (most recent call last):
  File "C:\fakepath\Test\test.py", line 94, in tearDownClass
    CheckFile.obj.terminate()
  File "C:\fakepath\Test\test.py", line 58, in terminate
    shutil.rmtree(self.unzip_dir, ignore_errors=False)
  File "C:\Program Files\Python39\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Program Files\Python39\lib\shutil.py", line 613, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Program Files\Python39\lib\shutil.py", line 618, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Program Files\Python39\lib\shutil.py", line 616, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\USERNAME\\AppData\\Local\\Temp\\tmplphs09bi\\resources\\testfile.dll'

I have double-checked that the process using the testfile.dll is freeing the dll after using it. I can even delete the file manually by going to that location - so that tells me it is not being used by anyone anymore. I have tried explicitly setting the permission for the file, and the parent folder. What could possibly be causing this, and how can this be resolved?

It might be worth noting that this class object is being created and destroyed upon the beginning and ending of a unittest in python.

EDIT : I found out that the permission is not being set even by calling os.chmod explicitly and recursively on the entire tree. The permission before and after calling chmod remains the same, would be glad if someone helped to identify why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文