Python实现指定文件夹的文件批量按序重命名
import os
# 文件夹路径
dir_path = './music'
# 文件扩展名
extent_name = 'mp3'
def read_dir(path, ext):
i = 1
# 路径是否存在
if not os.path.exists(path):
print('The path does not exit!')
else:
files = os.listdir(path) # 获取当前目录的所有文件及文件夹
for file in files:
new_name = str(i) + '.' + ext
os.rename(path+'/'+file, path+'/'+new_name)
i = i + 1
def main():
read_dir(dir_path, extent_name)
if __name__ == '__main__':
main()