[英]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.