如何在Python中重命名目录中的多个文件?

如何在Python中重命名目录中的多个文件?

如果您有一个文件列表,您想要重命名并提供相应的新文件名,则可以使用os模块的重命名方法。

阅读更多:Python 教程

例如

import os
for old, new in files.iteritems(): # files.items() in Python 3
    os.rename(old, new)

您还可以使用shutil(或shell实用程序)模块。调用shutil.move(source,destination)将移动路径source处的文件或文件夹到路径destination,并将返回新位置的绝对路径的字符串。

例如

import shutil
for old, new in files.iteritems(): # files.items() in Python 3
    shutil.move(old, new)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程