我遇到了一個問題,我試圖將文件從源文件移動到目標文件。該腳本第一次運行,但是當我再次運行它時,如果文件/目錄存在;它拋出這個錯誤如果文件和目錄存在
Traceback (most recent call last):
File "/Users/fela/Downloads/script.py", line 118, in save_pics
os.mkdir(dst_pics)
FileExistsError: [Errno 17] File exists: '/Users/dela/Downloads/Dest/Pictures/'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/fela/Downloads/script.py", line 385, in <module>
save_pics(msg, user_name)
File "/Users/fela/Downloads/script.py", line 120, in save_pics
shutil.move(png, dst_pics)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 542, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path '/Users/fela/Downloads/Dest/Pictures/image001.png' already exists
這裏是我的代碼:
src = /Users/fela/Downloads/Source
dst_pics = /Users/fela/Downloads/Dest/Pictures
for png in glob.iglob(os.path.join(src, "*.png")):
if os.path.isfile(png):
try:
os.mkdir(dst_pics)
except:
shutil.move(png, dst_pics)
continue
for jpg in glob.iglob(os.path.join(src, "*.[jJ][pP][gG]")):
if os.path.isfile(jpg):
try:
shutil.move(jpg, dst_pics)
except:
continue
問題是什麼?錯誤信息不清楚? –