加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python 文件夹复制

发布时间:2020-12-17 17:11:47 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #! /usr/bin/env python # -*- coding: utf-8 -*- #@author [email?protected] #@version 2010-09-25 14:57 import os import time sourceDir = r"

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#! /usr/bin/env python  
# -*- coding: utf-8 -*-  
#@author [email?protected]  
#@version 2010-09-25 14:57  

import os  
import time  

sourceDir = r"\192.168.3.250mmtimages" 
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',targetF)  
            else:  
                print u"%s %s 已存在,不重复复制" %(time.strftime('%Y-%m-%d %H:%M:%S',targetF)  

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

if __name__ == "__main__":  
    try:  
        import psyco  
        psyco.profile()  
    except ImportError:  
        pass 
    copyFiles(sourceDir,targetDir)

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读