[英]Permission denied on shutil.move on image files
I am trying to move some files around.我正在尝试移动一些文件。 I can move any extension type except .png, .jpg, or .gif.
我可以移动除 .png、.jpg 或 .gif 之外的任何扩展类型。 When I try to move those types of files I get "IOError: [Errno 13] Permission denied" even though I am the admin.
当我尝试移动这些类型的文件时,即使我是管理员,我也会收到“IOError: [Errno 13] Permission denied”。 Code below
下面的代码
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)
First things first , you're double escaping your dir
variable:首先,您要双重转义您的
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\*'
If you are still experiencing the error , it's because you are not giving the python script permissions to move the file.如果您仍然遇到错误,那是因为您没有授予python 脚本移动文件的权限。 There are a couple ways to get around this:
有几种方法可以解决这个问题:
(This applies to the asked question) (这适用于提出的问题)
Open command prompt (I see your file path and am assuming you're on windows) with administrative rights.使用管理权限打开命令提示符(我看到您的文件路径,并假设您在 Windows 上)。 ( see here )
( 见这里)
Change ownership of the images to you.将图像的所有权更改为您。 (see here for windows 10 or here for windows 7 )
(请参阅此处了解 Windows 10或此处了解 Windows 7 )
(This applies to people on Linux that may have the same problem) (这适用于 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
For more info, I used this site on permissions.有关更多信息,我在权限上使用了此站点。 (If someone has a numerical value than
755
for chmod please say so.) (如果有人的 chmod 数值超过
755
,请说出来。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.