【问题标题】:No such file or directory while using os.rename使用 os.rename 时没有这样的文件或目录
【发布时间】: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


【解决方案1】:

我发现了错误。

  • 实际上,某些图片的图片扩展名为 .jpg,而另一些图片的扩展名为 .JPG。

解决方案:

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

代码确实有效

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多