批量更改文件名

有一些工作是重复的、耗时间且很无聊的,那必然应该寻找一些工具,去执行这些重复的工作。人不应该去做机器可以做的事情。就像给一批文件重命名,自己逐个修改肯定是不靠谱的。

批量更改文件名

假如一个文件夹里面有几百个文件需要统一更改名字,手动理性肯定是不现实的,那采用程序应如何操作?

这里还是应用到最流利的程序语言Python

import os

# Specify the directory containing the files.
file_dir = r"file directory"

# Store files name of specified directory in a list.
file_list = os.listdir(file_dir)

for file_name in file_list:
    new_name = ii.replace('old_pattern','new_pattern')
    os.rename(os.path.join(file_dir, file_name), os.path.join(file_dir, new_name))

现在基本不用自己写程序了,直接问Chatbot便可以生成。例如:

import os

# Specify the directory containing the files
directory = "/path/to/directory"

# Loop through each file in the directory
for filename in os.listdir(directory):
    # Check if the file is a regular file
    if os.path.isfile(os.path.join(directory, filename)):
        # Perform the rename operation here
        new_filename = filename.replace("old_pattern", "new_pattern")
        
        # Rename the file
        os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))