(更新时间)2021年3月24日 python基础知识(批量修改文件名)

import os


def create_files():
    for i in range(10):
        file_name = 'test/file_' + str(i) + '.txt'
        print(file_name)
        f = open(file_name, 'w')
        f.close()


def create_files_1():
    os.chdir('test')
    for i in range(10, 20):
        file_name = 'file_' + str(i) + '.txt'
        print(file_name)
        f = open(file_name, 'w')
        f.close()
    os.chdir('../')  # ../ 上一级目录


def modify_filename():
    os.chdir('test')
    buf_list = os.listdir()
    # print(buf_list)
    for file in buf_list:
        new_file = 'py43_' + file
        os.rename(file, new_file)

    os.chdir('../')


def modify_filename_1():
    os.chdir('test')
    buf_list = os.listdir()
    # print(buf_list)
    for file in buf_list:
        num = len('py43_')
        new_file = file[num:]
        os.rename(file, new_file)

    os.chdir('../')


# create_files()
# create_files_1()
# modify_filename()
modify_filename_1()
上一篇:python黑科技库:FuckIt.py,让你代码从此远离bug


下一篇:JMS 之 Active MQ 启动嵌入式Broke