1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
def change_file_name(type, path, name1, name2=''):
'''
type:10=文件名前增加,11=文件名后增加,20删除关键词,30替换关键词
path:文件夹路径
name1:关键词1
name2:关键词2
'''
type = int(type)
old_list = os.listdir(path)
os.chdir(path)
if type == 10:
for i in old_list:
os.rename(i, name1 + i)
elif type == 11:
for i in old_list:
return_tuple = i.partition(".")
os.rename(i, return_tuple[0] + name1 + return_tuple[1] + return_tuple[2])
elif type == 20:
for i in old_list:
os.rename(i, i.replace(name1, ''))
elif type == 30:
for i in old_list:
os.rename(i, i.replace(name1, name2))
else:
return 'type错误'
new_list = os.listdir(path)
for i in old_list:
index = old_list.index(i)
print(i + ' => ' + new_list[index])
print('''
type:10=文件名前增加,11=文件名后增加,20删除关键词,30替换关键词
path:文件夹路径
name1:关键词1
name2:关键词2
''')
change_file_name(input('请输入type:'), input('请输入path:'), input('请输入name1:'), input('请输入name2(可空):'))