繁体   English   中英

使用os.rename时没有这样的文件或目录

[英]No such file or directory while using os.rename

这不是问题: 用[Errno2]重命名脚本没有这样的文件或目录

我正在尝试根据train和test文件夹中相应的xml文件名将图像从文件夹图像移动到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无法打开它

提前致谢!

我发现了错误。

  • 实际上,某些图像的扩展名为.jpg,而其他图像的扩展名为.JPG。

解:

  • 我将不太出现的.jpg手动更改为.JPG,并在代码中也将.jpg更改为.JPG。

该代码确实有效

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM