-
Notifications
You must be signed in to change notification settings - Fork 16
/
rename.py
executable file
·75 lines (62 loc) · 2.43 KB
/
rename.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 程序功能:遍历指定文件夹下的所有文件(包括子文件夹),删除文件名中的中的指定字符串
import os
import sys
def TraverseFolder(path):
no = 0
for (path, dirs, files) in os.walk(path):
no += 1
print '\n', "No.", no
print u"路径为:", path
if len(dirs) != 0:
subfolders = ''
# 将子文件夹名依次加入到字符串subfolders中来
for dir in dirs:
subfolders += dir+';'
subfolders = '[' + subfolders + ']'
print u"子文件夹名为:", subfolders
if len(files)!=0:
filenames = ''
for filename in files:
filenames += filename+';'
filenames = '[' + filenames + ']'
# print "files=", filenames
def RenameFile():
# path = raw_input(str.encode('gbk'))
path = '/Users/wowdd1/dev/xlb_env/xlinkBook/db'
#path = '/Users/zd/dev/python/course_env/xlinkBook/extensions'
# character = raw_input(str.encode('gbk'))
character = '2019'
year = '2020'
TraverseFolder(path)
print '\n', u'开始批量更名:'
print '-------------------------------------------------------'
changedCount = 0
for (path,dirs,files) in os.walk(path):
for filename in files:
srcfilename = os.path.splitext(filename)[0]
#print filename,'\n'
# scharacter = character - '.'
if (character in srcfilename):
#print srcfilename
'''
print changedCount
changedCount += 1
'''
newname = filename.replace(character, year)
#print newname
oldpath = path + "/" + filename
newpath = path + "/" + newname
print oldpath
print newpath
#'''
try:
os.rename(oldpath, newpath)
#print 'No.%d'%changedCount, 'change', oldpath, 'to', newpath
except BaseException, e:
print(str(e))
#'''
print '-------------------------------------------------------'
if __name__ == '__main__':
RenameFile()