【发布时间】:2018-11-04 12:30:10
【问题描述】:
这不是这个问题:Rename script with [Errno2] No such file or directory
我正在尝试根据 train 和 test 文件夹中相应的 xml 文件名将图像从文件夹 images 移动到 train_images 和 test_images 文件夹。
但始终出现此错误:
Traceback (most recent call last):
File "Move2.py", line 13, in <module>
os.rename(src2,dest2)
FileNotFoundError: [Errno 2] No such file or directory: '/home/mohit/darkflow/yes/images/DSCN8434.jpg' -> '/home/mohit/darkflow/yes/train_images/DSCN8434.jpg'
我的脚本是:
import os
train=os.listdir("/home/mohit/darkflow/yes/train")
test=os.listdir("/home/mohit/darkflow/yes/test")
path2="/home/mohit/darkflow/yes/images/"
moveto3="/home/mohit/darkflow/yes/train_images/"
moveto4="/home/mohit/darkflow/yes/test_images/"
for f_name in train:
f_name=f_name.replace(".xml",".jpg")
src2=path2+f_name
dest2=moveto3+f_name
os.rename(src2,dest2)
for f_name2 in test:
f_name2=f_name2.replace(".xml",".jpg")
src2=path2+f_name2
dest=moveto4+f_name2
os.rename(src2,dest2)
- 即使使用 shutil.move 也会发生类似的事情
- 我可以在生成错误的图像文件夹中看到该特定图像。
- 奇怪的是,当我在 images 目录中时,eog imagename 没有打开它
提前致谢!
【问题讨论】:
-
darkflow和/yes之间的空格从何而来?我在错误消息中看到了它们,但在您发布的代码中没有看到它们。 -
另外,请确保您的目标目录(文件夹)存在,即
train_images。 -
是的目录 train_images 存在,这些空间刚刚出现是因为我在堆栈溢出时的写作技巧不佳@TomKarzes
标签: python file-handling shutil