2

知道为什么下面的 Python 脚本在运行后将文件夹权限更改为只读吗?它运行一次并删除文件夹中的所有文件,但是当它再次运行时,它会出现 Windows 错误 5 访问被拒绝,因为脚本将权限更改为只读文件夹。我看不出它做了什么或如何避免它?谢谢!

import os
import shutil

for root, dirs, files in os.walk(eg.globals.tvzip):
    for f in files:
        os.remove(os.path.join(root, f))
    for d in dirs:
        shutil.rmtree(os.path.join(root, d))

for root, dirs, files in os.walk(eg.globals.tvproc):
    for f in files:
        os.remove(os.path.join(root, f))
    for d in dirs:
        shutil.rmtree(os.path.join(root, d))
4

2 回答 2

0

Not exactly sure why it's changing the folder permissions, I think it has something to do with Windows maybe.

Anyway you can change permissions before removing the files or directories

import stat
os.chmod(path, stat.S_IRWXU) #stat.S_IRWXU - Read, write, and execute by owner.
于 2013-11-04T14:33:09.320 回答
0

rmtree正在删除os.walk()期望接下来迭代的目录。所以我怀疑这是问题所在。

你可以topdown=Falsewalk通话中设置。或者你可以清除 dirs 数组。

于 2013-11-04T14:51:00.937 回答