所以我有一个很大的文件夹,它来自一个在线下载的数据集,几乎有5500个文件夹,每个文件夹包含6幅左右的图像。有些图像中有布面遮罩
因为有很多方法可以让我浏览其中的每一个文件夹,并尝试将文件移动到另一个目录,所以我尝试创建一个小脚本来完成这项任务,但当我尝试时,我得到了一个
Traceback (most recent call last):
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: '10K21D003-C11@2=cloth_front_mask.jpg' -> 'C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\cloth_mask\\10K21D003\\10K21D003-C11@2=cloth_front_mask.jpg'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#52>", line 9, in <module>
shutil.move(name, path, copy_function=shutil.copytree)
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 571, in move
copy_function(src, real_dst)
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 309, in copytree
names = os.listdir(src)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '10K21D003-C11@2=cloth_front_mask.jpg'
这是我的密码:
for root, dirs, files in os.walk('.'):
for name in files:
if 'mask' in name:
path = os.path.join('C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\cloth_mask', name[:9])
if os.path.exists(path):
pass
else:
os.mkdir(path)
shutil.move(name, path, copy_function=shutil.copytree)
我明白了。问题是在
shutil.move
行中我没有指定文件的原始文件夹。当我这么做的时候,效果很好相关问题 更多 >
编程相关推荐