批量重命名指定目录下的文件--python

 1  #Copyright 2012 wuyuangui.
 2  #contact : notmycupoftea@163.com
 3  
 4 
 5 
 6 import os
 7 
 8 path = 'F:\Python\open project\SGI\SGI_STL'
 9 paths = path.replace('\\', '//')
10 paths = paths + '//'
11 print paths
12 files = os.listdir(paths)
13 for i in files:
14     #print(i)
15     pat = None
16     pat = paths + str(i)
17     if os.path.isdir(pat):
18         pass
19     elif os.path.isfile(pat):
20         if not str(pat).endswith('.h'):
21             list = pat.split('//')
22             patt =  list[list.__len__() - 1] + '.h'
23             print(str(i) + str(patt))
24             os.rename(pat, str(pat + patt))
25             #os.rename(str(pat + patt), pat)
26                        
27         print('regfile:' + pat)
28     else:
29         pass
posted @ 2012-07-30 12:48  Yuangui  阅读(871)  评论(1编辑  收藏  举报