简体   繁体   English

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

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

This is not this question: Rename script with [Errno2] No such file or directory 这不是问题: 用[Errno2]重命名脚本没有这样的文件或目录

I am trying to move the images from the folder images to train_images and test_images folders on the basis of corresponding xml file names in train and test folders. 我正在尝试根据train和test文件夹中相应的xml文件名将图像从文件夹图像移动到train_images和test_images文件夹。 But getting this error consistently: 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' 但是始终得到此错误: 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'

My script is: 我的脚本是:

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)
  • Similar thing happens even if use shutil.move 即使使用shutil.move,也会发生类似的情况
  • I can see that particular image in the image folder for which error is generated. 我可以在生成错误的图像文件夹中看到该特定图像。
  • Strangely eog imagename does not open it when i am in the images directory 奇怪的是,当我在images目录中时,eog imagename无法打开它

Thanks in Advance! 提前致谢!

I found the mistake. 我发现了错误。

  • Actually the image extension was .jpg for some images and .JPG for others. 实际上,某些图像的扩展名为.jpg,而其他图像的扩展名为.JPG。

Solution: 解:

  • I changed the less appeared .jpg to .JPG manually and changed .jpg to .JPG in the code as well. 我将不太出现的.jpg手动更改为.JPG,并在代码中也将.jpg更改为.JPG。

The code did worked 该代码确实有效

Thank You! 谢谢!

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

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