如何使用 Python 重命名文件
我想将 a.txt
更改为 b.kml
。
I want to change a.txt
to b.kml
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想将 a.txt
更改为 b.kml
。
I want to change a.txt
to b.kml
.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(17)
使用
os.rename
:用法:
Use
os.rename
:Usage:
文件可能位于目录内,在这种情况下指定路径:
File may be inside a directory, in that case specify the path:
从 Python 3.4 开始,可以使用 pathlib 模块来解决这个问题。
如果您碰巧使用的是旧版本,则可以使用此处找到的向后移植版本
让我们假设你不在根路径中(只是为了增加一点难度)你想重命名,并且必须提供完整的路径,我们可以看看这个:
所以,你可以使用你的路径并创建一个
Path
对象:只是为了提供我们现在拥有的该对象的一些信息,我们可以从中提取内容。例如,如果出于某种原因我们想要通过将文件名从
the_file
修改为the_file_1
来重命名文件,那么我们可以获得文件名部分:并且仍然保留扩展名手以及:
我们可以通过简单的字符串操作来执行修改:
Python 3.6 及更高版本使用 f 字符串!
:
现在我们可以通过在我们创建的路径对象上调用
rename
方法并附加ext
来完成我们想要的正确重命名结构:否则 展示其简单性:
Python 3.6+:
低于 Python 3.6 的版本使用字符串格式方法:
As of Python 3.4 one can use the pathlib module to solve this.
If you happen to be on an older version, you can use the backported version found here
Let's assume you are not in the root path (just to add a bit of difficulty to it) you want to rename, and have to provide a full path, we can look at this:
So, you can take your path and create a
Path
object out of it:Just to provide some information around this object we have now, we can extract things out of it. For example, if for whatever reason we want to rename the file by modifying the filename from
the_file
tothe_file_1
, then we can get the filename part:And still hold the extension in hand as well:
We can perform our modification with a simple string manipulation:
Python 3.6 and greater make use of f-strings!
Otherwise:
And now we can perform our rename by calling the
rename
method on the path object we created and appending theext
to complete the proper rename structure we want:More shortly to showcase its simplicity:
Python 3.6+:
Versions less than Python 3.6 use the string format method instead:
这将有助于重命名或移动文件。
This will work to rename or move a file.
os.rename(old, new)
这可以在 Python 文档中找到: http://docs.python.org/library/os.html
os.rename(old, new)
This is found in the Python docs: http://docs.python.org/library/os.html
从 Python 3.3 及更高版本开始,通常首选使用 os.replace 而不是 os.rename,因此如果目标文件已存在。
请参阅文档。
As of Python version 3.3 and later, it is generally preferred to use
os.replace
instead ofos.rename
soFileExistsError
is not raised if the destination file already exists.See the documentation.
使用os.rename。但是您必须将两个文件的完整路径传递给该函数。如果我的桌面上有一个文件
a.txt
,那么我会这样做,而且我也必须提供完整的重命名文件。Use
os.rename
. But you have to pass full path of both files to the function. If I have a filea.txt
on my desktop so I will do and also I have to give full of renamed file too.这里需要注意的一点是,我们应该检查是否存在具有新文件名的文件。
假设如果 b.kml 文件存在,则重命名具有相同文件名的其他文件会导致删除现有的 b.kml。
One important point to note here, we should check if any files exists with the new filename.
suppose if b.kml file exists then renaming other file with the same filename leads to deletion of existing b.kml.
使用 Pathlib 库的 Path.rename 而不是 < code>os.rename:
主要好处是您不必构建绝对输出路径 - 您只需传递新文件名即可。第二个好处是,如果所有路径都已经是 Pathlib 对象,它可以保持统一。
Use the Pathlib library's Path.rename instead of
os.rename
:The primary benefit is that you don't have to construct the absolute output path — you can just pass the new filename. A secondary benefit is that it keeps things uniform if all your paths are all already Pathlib objects.
这是一个仅使用
pathlib
而不触及os
的示例,它基于字符串replace
操作更改目录中所有文件的名称,而不使用还有字符串连接:Here is an example using
pathlib
only without touchingos
which changes the names of all files in a directory, based on a stringreplace
operation without using also string concatenation:这应该可以做到。蟒蛇 3+
This should do it. python 3+
如何更改目录中文件名的首字母:
How to change the first letter of filename in a directory:
如果您使用的是Windows并且您想要重命名文件夹中的 1000 个文件,那么:
您可以使用下面的代码。 (Python3)
If you are Using Windows and you want to rename your 1000s of files in a folder then:
You can use the below code. (Python3)
os.chdir(r"D:\Folder1\Folder2")
os.rename(src,dst)
#src和dst应该在Folder2内
os.chdir(r"D:\Folder1\Folder2")
os.rename(src,dst)
#src and dst should be inside Folder2
您可以使用 os.system 调用终端来完成任务:
You can use os.system to invoke terminal to accomplish the task: