python批量重命名指定目录下所有文件的后缀名
有个需求,需要把某个文件夹下所有后缀名为.abc的更改为.xyz;
#批量重命名指定目录下面所有文件的后缀名。
word_dir为需要更改后缀的文件夹目录
old_ext为原文件最初的后缀名
new_ext为需要更改的新的后缀名
# batch_file_rename.py# Created: 6th August """This will batch rename a group of files in a given directory,once you pass the current and new extensions"""# just checking__author__ = Craig Richards\__version__ = 1.0import argparseimport osdef batch_rename(work_dir, old_ext, new_ext):"""This will batch rename a group of files in a given directory,once you pass the current and new extensions"""# files = os.listdir(work_dir)for filename in os.listdir(work_dir):# Get the file extensionsplit_file = os.path.splitext(filename)# Unpack tuple elementroot_name, file_ext = split_file# Start of the logic