简体   繁体   English

Python 说它找不到我要重命名的文件,即使它可以读取文件名

[英]Python says it can't find the file I am trying to rename even though it can read the file name

Im tryin to append a short string to every '.png' file.我试图在每个“.png”文件中附加一个短字符串。 But when I run it, it says cannot find the file.但是当我运行它时,它说找不到文件。 But I know it's there and I can see it in the folder.但我知道它在那里,我可以在文件夹中看到它。

Is there anything I need to do?有什么我需要做的吗?

Here is my script:这是我的脚本:

import os

for file in os.listdir("./pics"):
    if file.endswith(".png"):
        newFileName = "{0}_{2}{1}".format(*os.path.splitext(file) + ("z4",))
        os.rename(file, newFileName)

Here is the error message I get...02.png is the first file in the folder:这是我收到的错误消息...02.png 是文件夹中的第一个文件:

fileNotFoundError: [WinError 2] The system cannot find the file specified: '02.png' -> '02_z4.png' fileNotFoundError: [WinError 2] 系统找不到指定的文件:'02.png' -> '02_z4.png'

It's odd though because it gets the filename, in this case, 02.png .不过这很奇怪,因为它获取了文件名,在本例中为02.png So if it can read the file name, why can't it find it?那么如果它可以读取文件名,为什么找不到它呢?

Thanks!谢谢!

I thought my comment might be enough, but for clarity I'll provide a short answer.我认为我的评论可能已经足够了,但为了清楚起见,我将提供一个简短的答案。

02.png doesn't exist relative to your working directory. 02.png相对于您的工作目录不存在。 You need to specify the path to the file for os.rename so you need to include the directory.您需要为os.rename指定文件的路径,因此您需要包含该目录。

import os

for file in os.listdir("./pics"):
    if file.endswith(".png"):
        newFileName = "/pics/{0}_{2}{1}".format(*os.path.splitext(file) + ("z4",)) # Notice the ./pics
        os.rename(os.path.join('pics', file), newFileName)

The name returned from os.listdir() gives the filename, not the full path.从 os.listdir() 返回的名称给出了文件名,而不是完整路径。 So you need to rename pics/02.png to pics/02_zf.png.所以你需要将 pics/02.png 重命名为 pics/02_zf.png。 Right now you don't include the directory name.现在您不包括目录名称。

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

相关问题 Docker 容器找不到文件,即使我看到它 - Docker container can't find file even though I see it 即使它们在同一目录中也找不到 JSON 文件 - Can't find a JSON file even though they are in the same directory 无法让我的 python 代码找到 jpg 文件,即使它位于正确的文件夹中 - Can't get my python code to find jpg file even though it is located in the correct folder 无法写入文件,即使我有权这样做在 Python - Can't write to file, even though I have permissions to do so In Python Python Pandas 虽然引擎是 xlrd,但无法读取 .xls 文件 - Python Pandas can't read .xls file though engine is xlrd 尝试从 opencv 中的 .mov 视频读取帧时“找不到起始编号(以文件名)” - “Can't find starting number (in the name of file)” when trying to read frames from .mov video in opencv 即使安装了Python,也找不到PyGObject / gi - Python can't find PyGObject/gi even though it is installed 即使导入成功,Python也找不到类 - Python can't find class even though the import was successful Playsound python 模块即使在同一文件夹中也找不到文件 - Playsound python module can't find file even in same folder 即使我有python setup.py开发它也无法按库名导入 - Can't import by library name, even though I have python setup.py develop it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM