我正试着移动一些文件。我可以移动除.png、.jpg或.gif以外的任何扩展类型。当我尝试移动这些类型的文件时,我得到“IOError:[Errno 13]Permission denied”,即使我是管理员。下面的代码
import os, glob, shutil
dir = r'C:\\Users\\jcan4\\Desktop\\testmove\\*'
print(dir)
files = glob.glob(dir)
files.sort(key=os.path.getmtime)
for i, file in enumerate(files, start=1):
print(file)
oldext = os.path.splitext(file)[1]
shutil.move(file, 'Attachment-%s' % (i) + oldext)
第一件事优先,您要双重转义
dir
变量:如果您仍然遇到错误,那是因为您没有授予python脚本移动文件的权限。有几种方法可以解决这个问题:
窗户
(这适用于提问)
以管理权限打开命令提示符(我看到了您的文件路径,并且假设您在windows上)。(see here)
将图像的所有权更改为您。(见here for windows 10或here for windows 7)
Linux(MacOS)
(这适用于Linux上可能有相同问题的用户)
有关更多信息,我在权限上使用了this site。(如果有人的chmod值大于
755
,请这样说。)相关问题 更多 >
编程相关推荐