简体   繁体   English

Python更改文件夹中多个文件的文件名

[英]Python changing filenames of multiple files in a folder

I'm trying to make a script to rename all files in a folder and this is the code I have, but it doesn't work. 我正在尝试制作一个脚本来重命名文件夹中的所有文件,这是我的代码,但它不起作用。 I have like 200 images in that folder and I want to change filename1_ to filename1 and so on for all the files. 我在该文件夹中有200个图像,我想将filename1_更改为filename1,依此类推所有文件。 The transformation is dog_1 to doggy1; 转变是dog_1到doggy1; or whatever, the new name of the file can be anything. 或者其他什么,文件的新名称可以是任何东西。 Could somebody help me out? 有人可以帮帮我吗? I'm still new to python so the code I have right now might look like nothing. 我还是python的新手,所以我现在的代码可能看起来没什么。

import os
    directoryname="C:\\Users\\Ineke\\Documents\\Python Scripts\\images"
    lijstmetfiles = os.listdir(directoryname)
    for i in range(len(lijstmetfiles)):
        os.rename(str(lijstmetfiles[i]), str("doggy"+str(i))

I ran your code and here are your two main problems: 我运行了你的代码,这是你的两个主要问题:

The first being, you are not closing your bracket on your os.rename line. 第一个是,你没有关闭os.rename行的括号。

You have this: 你有这个:

os.rename(str(lijstmetfiles[i]), str("doggy"+str(i))

You are missing a parentheses, so should be: 你错过了一个括号,所以应该是:

os.rename(str(lijstmetfiles[i]), str("doggy"+str(i)))

However, this could be an edit issue from copying your code over in to your post. 但是,这可能是将代码复制到帖子中的编辑问题。

Secondly, and most importantly, you are not specifying the path you want to do your rename on, you are just giving two filenames, so you are most likely getting a file not found error. 其次,最重要的是,您没有指定要重命名的路径,您只是提供两个文件名,因此您很可能会收到文件未找到错误。

You need to make use of Python's os.path.join method using your directory and the filename, as in the code sample below: 您需要使用您的目录和文件名来使用Python的os.path.join方法,如下面的代码示例所示:

os.rename(
    os.path.join(directoryname, str(lijstmetfiles[i])),
    os.path.join(directoryname, str("doggy"+str(i)))
    ) 

So now you are explicitly specifying what the full path is for your rename. 所以现在您明确指定重命名的完整路径。

Another point to make, you don't need to be casting to a "str" for your filenames. 另一个要点,你不需要为你的文件名强制转换为“str”。 Even if your filename is 5 for example, getting the list of files from listdir will still come back as a string. 例如,即使您的文件名为5 ,从listdir获取文件列表仍将以字符串形式返回。

Finally, putting it all together, your code should look something like this: 最后,把它们放在一起,你的代码应该是这样的:

import os
directoryname="C:\\Users\\Ineke\\Documents\\Python Scripts\\images"
lijstmetfiles = os.listdir(directoryname)
print(lijstmetfiles)
for i in range(len(lijstmetfiles)):
    os.rename(
        os.path.join(directoryname, lijstmetfiles[i]),
        os.path.join(directoryname, "doggy"+str(i))
        )

I tested this on my end and it should work. 我在我的最后测试了它,它应该工作。

Here is documentation on the os module. 这是os模块的文档。 Take a look at it to get a further understanding of what is available to you when playing with the filesystem: 看一下它,以便进一步了解在使用文件系统时可以使用的内容:

Python 2: https://docs.python.org/2/library/os.html Python 2: https//docs.python.org/2/library/os.html

Python 3: https://docs.python.org/3/library/os.html Python 3: https//docs.python.org/3/library/os.html

The following code will rename all of the files in that directory with similar names but with _ excluded. 以下代码将使用相似的名称重命名该目录中的所有文件,但使用_ excluded。

#UNTESTED
import os
directoryname="C:\\Users\\Ineke\\Documents\\Python Scripts\\images"
lijstmetfiles = os.listdir(directoryname)
for i in lijstmetfiles:
    os.rename(os.path.join(directoryname, i),
              os.path.join(directoryname,
                           i.replace('_','')))

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

相关问题 Python / Pandas-根据文件名中的日期读取文件夹中的多个文件 - Python/Pandas - Read multiple files in a folder based on dates in filenames Python:根据文件名将文件移动到文件夹 - Python: Moving files to folder based on filenames Python 3.5:根据文件名将文件移动到文件夹 - Python 3.5: Moving files to folder based on filenames Python,easyGUI和sqlite3:使用python读取一个文件夹或多个文件夹中的文件,然后将文件夹名称和文件名添加到数据库 - Python, easyGUI and sqlite3: Using python to read files in one folder or multiple folder, then adding the folder names and filenames to database Python:更改所有文件夹和子文件夹中的文件名和文件夹名 - Python: Changing filenames and folder names in all folders and subfolders 在python中读取多个文件并将文件名和内容组合成一个数据帧 - Read multiple files in python and combine filenames and content into a dataframe 如何通过跟随文件名的文本列表在Python中打开多个文件? - How to open multiple files in Python by following a text list of filenames? Python匹配文件名并放入文件夹 - Python match filenames and put in a folder 导出具有不同文件名的多个文件 - Exporting multiple files with different filenames Python - 从文件夹中更改 .txt 文件的内容并保存在新文件夹中 - Python - changing content of .txt files from folder and saving in new folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM