简体   繁体   English

尝试重命名目录中的所有文件时出现 FileNotFoundError

[英]FileNotFoundError when trying to rename all the files in a directory

I am writing a python script to rename all files in a given folder.我正在编写一个 python 脚本来重命名给定文件夹中的所有文件。 The python script exists in my jl-classifier along with images/jaguar file. python 脚本与images/jaguar文件一起存在于我的jl-classifier I'm trying to run the following script to take each file in the folder and rename it to this format:我正在尝试运行以下脚本以获取文件夹中的每个文件并将其重命名为这种格式:

jaguar_[#].jpg

But its throwing the following error:但它抛出以下错误:

Traceback (most recent call last):
  File "/home/onur/jaguar-leopard-classifier/file.py", line 14, in <module>
    main()
  File "/home/onur/jaguar-leopard-classifier/file.py", line 9, in main
    os.rename(filename, "Jaguar_" + str(x) + file_ext)
FileNotFoundError: [Errno 2] No such file or directory: '406.Black+Leopard+Best+Shot.jpg' -> 'Jaguar_0.jpg'

This is my code:这是我的代码:

import os


def main():
    x = 0
    file_ext = ".jpg"

    for filename in os.listdir("images/jaguar"):
        os.rename(filename, "Jaguar_" + str(x) + file_ext)
        x += 1


if __name__ == '__main__':
    main()

os.listdir only returns the filename (not the file path...) os.listdir只返回文件名(不是文件路径...)

try the following尝试以下

for filename in os.listdir("images/jaguar"):
    filepath = os.path.join("images/jaguar",filename)
    new_filepath = os.path.join("images/jaguar","Jaguar_{0}{1}".format(x,file_ext))
    os.rename(filepath, new_filepath)

being explicit is almost always a path to a happier life直言不讳几乎总是通往更幸福生活的道路

In order to use os.rename() , you need to provide absolute paths.为了使用os.rename() ,您需要提供绝对路径。

I would suggest replacing line 9 with os.rename(os.path.expanduser(f"~/{whatever folders you have here}/images/jaguar/{filename}"), os.path.expanduser(f"~/{whatever folders you have here}/images/jaguar/Jaguar_{str(x)}{file_ext}")我建议将第 9 行替换为os.rename(os.path.expanduser(f"~/{whatever folders you have here}/images/jaguar/{filename}"), os.path.expanduser(f"~/{whatever folders you have here}/images/jaguar/Jaguar_{str(x)}{file_ext}")

os.path.expanduser() allows you to use the "~" syntax to aid the abs file path. os.path.expanduser()允许您使用“~”语法来帮助 abs 文件路径。

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

相关问题 尝试使用os.rename时出现FileNotFoundError - FileNotFoundError when trying to use os.rename 尝试将目录中的图像从 JPG 转换为 PNG 文件时出现“FileNotFoundError: [Errno 2] 没有这样的文件或目录” - 'FileNotFoundError: [Errno 2] No such file or directory' when trying to convert images in directory from JPG to PNG files 尝试将文件移动到临时目录时出现 FileNotFoundError - FileNotFoundError when attempting to move files into a temp directory 尝试写入文件时出现“ FileNotFoundError:[Errno 2]没有这样的文件或目录” - “FileNotFoundError: [Errno 2] No such file or directory” when trying to write to file FileNotFoundError: [Errno 2] 尝试使用本地包时没有这样的文件或目录 - FileNotFoundError: [Errno 2] No such file or directory when trying to use local package DJANGO - FileNotFoundError: [Errno 2] No such file or directory: '' 尝试访问文件时 - DJANGO - FileNotFoundError: [Errno 2] No such file or directory: '' when trying to acess a file 用Python中的连续数字重命名目录中的所有文件 - Rename All Files in a Directory with consecutive numbers in Python 无法重命名目录中的所有文件 - Can't rename all files in a directory 如何重命名所有文件以包含目录名称? - How to rename all files to include the directory name? 使用Python重命名目录中的所有文件 - Rename All Files in a Directory Using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM