[英]Permission denied on shutil.move on image files
我正在尝试移动一些文件。 我可以移动除 .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
变量:
print(r'C:\\Users\\jcan4\\Desktop\\testmove\\*')
# Yields 'C:\\\\Users\\\\jcan4\\\\Desktop\\\\testmove\\\\*' !!
# What you really meant was either one of the following:
dir_harderToRead = 'C:\\Users\\jcan4\\Desktop\\testmove\\*'
dir_easyToRead = r'C:\Users\jcan4\Desktop\testmove\*'
如果您仍然遇到错误,那是因为您没有授予python 脚本移动文件的权限。 有几种方法可以解决这个问题:
(这适用于提出的问题)
使用管理权限打开命令提示符(我看到您的文件路径,并假设您在 Windows 上)。 ( 见这里)
将图像的所有权更改为您。 (请参阅此处了解 Windows 10或此处了解 Windows 7 )
(这适用于 Linux 上可能有同样问题的人)
# At command line
sudo python your_script_name.py
# At command line
# Changes ownership of entire directory (CAREFUL):
chmod 755 /absolute/path/to/dir
chmod 755 relative/path/to/dir
# Or you can change file by file:
chmod 755 /absolute/path/to/file
chmod 755 relative/path/to/file
有关更多信息,我在权限上使用了此站点。 (如果有人的 chmod 数值超过755
,请说出来。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.