【发布时间】:2025-12-05 07:30:01
【问题描述】:
我正在尝试在我的 USB 驱动器中查找“jpg、jpeg、png”文件,并尝试将它们一一移动到新文件夹中。当我尝试手动将它移动一个文件时,它可以工作,但是当我运行以下程序时它失败了。请让我知道这里有什么问题。
import re
import os
import ntpath as path
import shutil
path="E:\\Mac"
newpath="E\\Mac\\MovedPics"
os.chdir(path)
expr=r'\.(jpg|JPG|jpeg|JPEG|png|PNG)$'
for file in os.listdir(path):
if os.path.isfile(file):
match=re.search(expr,file)
if match:
abspath=os.path.abspath(file)
print('REGEXP MATCHED :-',abspath)
move=shutil.move(abspath,newpath)
if move:
print('MOVE SUCCESSFUL :-',file)
else:
print('MOVE FAILED:-',file)
break
else:
print('DESTINATION DIR ',newpath, ' DOESNT EXIST', file,':', os.getcwd())
错误:-
DESTINATION DIR E\Mac\MovedPics 不存在 voice_instructions_imperial 2.zip : E:\Mac 目标目录 E\Mac\MovedPics 不存在 usbpicsdata.txt : E:\Mac 正则表达式匹配:- E:\Mac\tattoo4.jpg 回溯(最近一次通话最后): 文件“C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py”,第 544 行,在移动中 os.rename(src, real_dst) FileNotFoundError: [WinError 3] 系统找不到指定路径:'E:\Mac\tattoo4.jpg' -> 'E\Mac\MovedPics'
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次): 文件“”,第 7 行,在 move=shutil.move(abspath,newpath) 文件“C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py”,第 558 行,在移动中 复制函数(src,real_dst) 文件“C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py”,第 257 行,在 copy2 复制文件(src,dst,follow_symlinks=follow_symlinks) 复制文件中的文件“C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py”,第 121 行 使用 open(dst, 'wb') 作为 fdst: FileNotFoundError: [Errno 2] 没有这样的文件或目录:'E\Mac\MovedPics'
【问题讨论】:
-
当我尝试手动移动该文件时,它可以工作。 >>shutil.move('E:\\Mac\\tattoo4.jpg','E:\\Mac\\MovedPics') 'E:\\Mac\\MovedPics\\tattoo4.jpg' >>>跨度>
标签: python python-3.x