Python 文件夾復制

ex7n 9年前發布 | 1K 次閱讀 Python

#! /usr/bin/env python

-- coding: utf-8 --

@author zcwang3@gmail.com

@version 2010-09-25 14:57

import os
import time

sourceDir = r"\\192.168.3.250\mmtimages" targetDir = r"D:\mmtimages" copyFileCounts = 0

def copyFiles(sourceDir, targetDir):
global copyFileCounts
print sourceDir
print u"%s 當前處理文件夾%s已處理%s 個文件" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), sourceDir,copyFileCounts)
for f in os.listdir(sourceDir):
sourceF = os.path.join(sourceDir, f)
targetF = os.path.join(targetDir, f)

    if os.path.isfile(sourceF):  
        #創建目錄  
        if not os.path.exists(targetDir):  
            os.makedirs(targetDir)  
        copyFileCounts += 1 

        #文件不存在,或者存在但是大小不同,覆蓋  
        if not os.path.exists(targetF) or (os.path.exists(targetF) and (os.path.getsize(targetF) != os.path.getsize(sourceF))):  
            #2進制文件  
            open(targetF, "wb").write(open(sourceF, "rb").read())  
            print u"%s %s 復制完畢" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)  
        else:  
            print u"%s %s 已存在,不重復復制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)  

    if os.path.isdir(sourceF):  
        copyFiles(sourceF, targetF)  

if name == "main":
try:
import psyco
psyco.profile()
except ImportError:
pass copyFiles(sourceDir,targetDir)</pre>

 本文由用戶 ex7n 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!