我是 Python 新手,正在尝试按数字顺序重命名图像后将照片 (.jpg) 复制到文件夹中。我编写了以下脚本,将目录中的所有照片(全部 32,000 张)重命名为它们的名称(杂草):
import os
os.chdir('E:\\weeds')
i=1
for file in os.listdir():
src=file
dst="weed"+str(i)+".jpg"
os.rename(src,dst)
i+=1
以下是输出命名的示例:
然后我尝试将前 250 张照片复制到一个新目录中,如下所示:
import os, shutil
#Copying files to folders
original_weed = 'E:\\weeds'
train_weed = 'E:\\weeds_train'
#Training dataset: Weeds
fnames = ['weed{}.jpg'.format(i) for i in range(250)]
for fname in fnames:
src = os.path.join(original_weed, fname)
dst = os.path.join(train_weed, fname)
shutil.copyfile(src, dst)
产生以下错误:
FileNotFoundError Traceback (most recent call last)
<ipython-input-7-f08fff292dd8> in <module>
17 src = os.path.join(original_weed, fname)
18 dst = os.path.join(train_weed, fname)
---> 19 shutil.copyfile(src, dst)
~\anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
259 os.symlink(os.readlink(src), dst)
260 else:
--> 261 with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
262 # macOS
263 if _HAS_FCOPYFILE:
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\weed0.jpg'
我需要做什么才能识别要复制的文件?
慕雪6442864
相关分类