基本方法
import os
import shutil
alllist=os.listdir(u"D:\\notes\\python\\资料\\")
for i in alllist:
aa,bb=i.split(".")
if 'python' in aa.lower():
oldname= u"D:\\notes\\python\\资料\\"+aa+"."+bb
newname=u"d:\\copy\\newname"+aa+"."+bb
shutil.copyfile(oldname,newname)
比较全面的方法
#coding:utf-8 import os import sys import getpass import shutil
# shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件
# 创建多级目录:os.makedirs("/Users/ximi/version")
# 创建单个目录:os.mkdir("project")
# #复制文件
# shutil.copyfile('listfile.py', 'd:/test.py')
# shutil.rmtree("dir") 空目录、有内容的目录都可以删
# 检验给出的路径是否真地存:os.path.exists()
# getpass.getuser()该函数返回登陆的用户名,不需要参数
username = getpass.getuser()
# 改变当前工作目录
os.chdir('/Users/' + username + '/Documents/client/myProj/')
# shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件
# 创建多级目录:os.makedirs("/Users/ximi/version")
# 创建单个目录:os.mkdir("project")
# #复制文件
# shutil.copyfile('listfile.py', 'd:/test.py')
# shutil.rmtree("dir") 空目录、有内容的目录都可以删
# 检验给出的路径是否真地存:os.path.exists()
# getpass.getuser()该函数返回登陆的用户名,不需要参数
username = getpass.getuser()
# 改变当前工作目录
os.chdir('/Users/' + username + '/Documents/client/myProj/')
文件的拷贝用shutil.copyfile(srcFilePath,dstFilePath)
def handleVersionFile():
# os.getcwd()获取当前工作目录,即当前python脚本工作的目录路径。 srcVersionFilePath = os.getcwd()+os.sep+"res/version/version.manifest" dstVersionFilePath = os.getcwd()+os.sep+"tools/myProj/version/version.manifest" versionDir = os.getcwd()+os.sep+"tools/myProj/version/" if not os.path.exists(versionDir): print versionDir, '\n配置文件目录不存在,创建目录...' # os.mkdir(versionDir) os.makedirs(versionDir) print '创建配置文件目录成功!\n' srcProjectFilePath = os.getcwd()+os.sep+"res/version/project.manifest" dstProjectFilePath = os.getcwd()+os.sep+"tools/myProj/version/project.manifest" print '拷贝配置文件开始...' if os.path.exists(srcVersionFilePath): shutil.copyfile(srcVersionFilePath,dstVersionFilePath) if os.path.exists(srcProjectFilePath): shutil.copyfile(srcProjectFilePath,dstProjectFilePath) print '拷贝配置文件结束!\n'
文件夹的拷贝用shutil.copytree(dstResDir)
def handleAssetsFile(): sourceSrcDir = os.getcwd()+os.sep+"src/" dstSrcDir = os.getcwd()+os.sep+"tools/myProj/assets/src/" sourceResDir = os.getcwd()+os.sep+"res/" dstResDir = os.getcwd()+os.sep+"tools/myProj/assets/res/" #复制目录,olddir和newdir都只能是目录,且newdir必须不存在 if os.path.exists(dstSrcDir): print dstSrcDir, '存在先删除'
# 如果要递归删除目录的内容,可使用shutil.rmtree()函数 shutil.rmtree(dstSrcDir) print '拷贝代码文件夹开始...' shutil.copytree(sourceSrcDir, dstSrcDir) print '拷贝代码文件夹结束!\n' if os.path.exists(dstResDir): print dstResDir, '存在先删除' shutil.rmtree(dstResDir) print '拷贝资源文件夹开始...' shutil.copytree(sourceResDir, dstResDir) print '拷贝资源文件夹结束!\n'
if __name__ == "__main__":
handleVersionFile()
handleAssetsFile()