shutil.copy()、os.walk()、os.rename()实例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import shutil
Path = "panel/"
PNPath = "pn/"
for dirpath, dirnames, filename in os.walk(Path):
for panelfile in filename:
panelfilePath = dirpath+"/"+panelfile
if panelfile != "panel_setting.h":
if (os.path.exists(panelfilePath)):
os.remove(panelfilePath)
else:
print "要删除的文件不存在!"
for filename1 in os.listdir(PNPath):
#将文件拷贝到指定文件
shutil.copy(PNPath+'\\'+filename1, dirpath)
#对于目录进行重命名
for dir in os.listdir(Path):
dirpath = Path+'/'+dir
if os.path.isdir(dirpath):
#注意对于rename的参数,如果oldname没有在当前目录下,参数需要加路径
os.rename(dirpath,Path+"PN_COMMON_"+dir)
else:
print "%s不是目录" % dir