[英]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.