python重命名文件夹

99 阅读1分钟
import os

# 文件夹路径
directory = '05 spider(web crawler program)'

# 获取子文件夹列表
folder_names = [f for f in os.listdir(directory) if os.path.isdir(os.path.join(directory, f))]

# 新的文件夹名列表
new_names = ['043 Translation', '044 get video', '045 get a picture','046 get the weather','047 get the content','048 get the story',
             '049 get picture','050 get news headlines and content','051 Automatic login']

# 遍历子文件夹列表
for i in range(len(folder_names)):
    # 原始文件夹路径
    original_path = os.path.join(directory, folder_names[i])
    # 新的文件夹路径
    new_path = os.path.join(directory, new_names[i])

    # 重命名文件夹
    os.rename(original_path, new_path)