# -*- 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_())