简体   繁体   中英

No such file or directory while using os.rename

This is not this question: Rename script with [Errno2] No such file or directory

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. 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'

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
  • 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

Thanks in Advance!

I found the mistake.

  • Actually the image extension was .jpg for some images and .JPG for others.

Solution:

  • I changed the less appeared .jpg to .JPG manually and changed .jpg to .JPG in the code as well.

The code did worked

Thank You!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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