簡體   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