代码如下:
#encoding=utf-8
print '中国'
#在目录树中改变文件扩展名 重命名一系列指定类型的 文件
import os
str ='123'
def swapextensions(dir,before,after):
if before[:1] != '.':
before = '.'+before
thelen = -len(before)
if after[:1] != '.':
after = '.'+after
for path, subdirs, files in os.walk(dir):
for oldfile in files:
if oldfile[thelen:] == before:
oldfile = os.path.join(path,oldfile)
newfile = oldfile[:thelen] + after
os.rename(oldfile, newfile)
#测试
import os, fnmatch
def all_files(root, patterns='*', single_level=False, yield_folders=False):
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders: #枚举目录
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name , pattern):
yield os.path.join(path,name)
break
if single_level:
break
print '-------1'
for path in all_files(r'H:\football\GetFootballMatch\Release'):
print path.decode('gbk')
swapextensions(r'H:\football\GetFootballMatch\Release',r'exe',r'exd')
print '-------2'
for path in all_files(r'H:\football\GetFootballMatch\Release'):
print path.decode('gbk')
print '-------3'
swapextensions(r'H:\football\GetFootballMatch\Release',r'exd',r'exe')
for path in all_files(r'H:\football\GetFootballMatch\Release'):
print path.decode('gbk')
打印结果如下:
中国
-------1
H:\football\GetFootballMatch\Release\ft.ini
H:\football\GetFootballMatch\Release\足彩辅助.exe
H:\football\GetFootballMatch\Release\足彩辅助.pdb
H:\football\GetFootballMatch\Release\足彩辅助.zip
H:\football\GetFootballMatch\Release\足彩辅助\足彩辅助.exe
-------2
H:\football\GetFootballMatch\Release\ft.ini
H:\football\GetFootballMatch\Release\足彩辅助.exd
H:\football\GetFootballMatch\Release\足彩辅助.pdb
H:\football\GetFootballMatch\Release\足彩辅助.zip
H:\football\GetFootballMatch\Release\足彩辅助\足彩辅助.exd
-------3
H:\football\GetFootballMatch\Release\ft.ini
H:\football\GetFootballMatch\Release\足彩辅助.exe
H:\football\GetFootballMatch\Release\足彩辅助.pdb
H:\football\GetFootballMatch\Release\足彩辅助.zip
H:\football\GetFootballMatch\Release\足彩辅助\足彩辅助.exe