# 文件存放的路径 path = r"./" # 将命令中第二个参数作为输入,多个参数空格隔开 if len(sys.argv) >= 2: base = sys.argv[1] else: # 控制台输入 base = input('Please input base name: ')
print("You inputted ", base)
# 遍历更改文件名 num = 1
for old in os.listdir(path): if old == 'rename.py': print('Skip script!') else: new = base + '_' + str(num) + os.path.splitext(old)[-1] os.rename(os.path.join(path, old), os.path.join(path, new)) print('Rename', old, 'to', new) num = num + 1