py copy file

# -*- coding: utf-8 -*-
import os, sys, time, shutil

start = time.time()

sourceDir = r"D:\cache\Adobe Photoshop CC 2015\AMT"
temp_sourceDir = sourceDir.split("\\")[-1]
temp_targetDir = r"D:\cache\ddd\B"
targetDir = os.path.join(temp_targetDir, temp_sourceDir)

copyFileCounts = 0


def copyFiles(sourceDir, targetDir):
    global copyFileCounts
    print (sourceDir)
    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
            shutil.copy(sourceF, targetF)

            print (u"%s %s 复制完毕" % (time.strftime('%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)

end = time.time()
timeX = end - start
print (u"%d个文件 共运行了 %.2f 秒:" % (copyFileCounts, timeX))

带进度条

# -*- coding: utf-8 -*-
import os, sys, time, shutil, threading, stat

try:
    from PyQt4 import QtCore, QtGui
    from PyQt4 import *
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
except ImportError:
    from PyQt5 import QtCore, QtGui
    from PyQt5 import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QWidget import *

sourceDir = r"D:\cache\A"
temp_targetDir = r"D:\cache\B"
temp_sourceDir = sourceDir.split("\\")[-1]
targetDir = os.path.join(temp_targetDir, temp_sourceDir)


class copyDailies(QDialog):
    def __init__(self, sourceDir, targetDir,parent = 0):
        super(copyDailies, self).__init__()
        self.sourceDir = sourceDir
        self.targetDir = targetDir
        self.copyFileCounts = 0

    #进度条窗口
        self.progressDialog = QProgressDialog(self)
        self.progressDialog.setWindowModality(Qt.WindowModal)
        self.progressDialog.setMinimumDuration(1)
        self.progressDialog.setWindowTitle(u"请等待")
        self.progressDialog.setLabelText(u"拷贝...")
        self.progressDialog.setCancelButtonText(u"取消")

    def copyFiles(self):
        self.timestartX = time.time()
        print (self.sourceDir)
        for f in os.listdir(self.sourceDir):

            sourceF = os.path.join(self.sourceDir, f)
            targetF = os.path.join(self.targetDir, f)

            if os.path.isfile(sourceF):
                if not os.path.exists(self.targetDir):
                    os.makedirs(self.targetDir)
                self.copyFileCounts += 1
                shutil.copy(sourceF, targetF)
                print (u"%s %s 复制完成" % (time.strftime('%H:%M:%S', time.localtime(time.time())), targetF))
            if os.path.isdir(sourceF):
                print (u"文件夹复制中.....")
                if os.path.exists(targetF):
                    os.chmod(targetF, stat.S_IWRITE) #设置权限
                    shutil.rmtree(targetF)
                self.copyFileCounts += 1
                shutil.copytree(sourceF, targetF)
                print (u"%s %s 复制完成" % (time.strftime('%H:%M:%S', time.localtime(time.time())), targetF))

            #设置进度条
            # 设置文件数量
            num = self.copyFileCounts
            for i in range(num):
                self.progressDialog.setValue(i)
                if self.progressDialog.wasCanceled():
                    print (u"进程已中断,总共复制了%s个文件 " % self.copyFileCounts)
                    sys.exit()
                    # return

        num = self.copyFileCounts
        # 设置进度条范围
        self.progressDialog.setRange(0, num)
        # 显示进度条
        self.progressDialog.show()


        self.timeend = time.time()
        timeX = self.timeend - self.timestartX
        print (u"%s个文件,运行了%.3f秒" % (self.copyFileCounts, timeX))
        sys.exit()




def return_copy():
    copy = copyDailies(sourceDir, targetDir)
    copy.copyFiles()


def copy_main():
    a = threading.Thread(target=return_copy)
    # a.setDaemon(True)
    a.start()


if __name__ == "__main__":
    app=QtGui.QApplication(sys.argv)
    return_copy()
    sys.exit(app.exec_())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值