遍历文件夹下所有txt文件并重命名

遍历文件夹下所有txt文件并重命名_第1张图片

import os

filenames = os.listdir('.')
path_files = []
for filename in filenames:
    filename = filename.split('.')
    if filename[-1] == 'log':
        old_file_name = str.join('.',filename)
        new_file_name = filename[0] + '_' + filename[1] + '.txt'
        path_file = "./" + old_file_name
        os.rename(path_file,new_file_name)
        path_files.append(path_file)

遍历文件夹下所有txt文件并重命名_第2张图片

你可能感兴趣的:(Python3.5)