python實(shí)現(xiàn)復(fù)制大量文件功能
本文實(shí)例為大家分享了python實(shí)現(xiàn)復(fù)制大量文件的具體代碼,供大家參考,具體內(nèi)容如下
本來(lái)是去項(xiàng)目公司拷數(shù)據(jù),結(jié)果去了發(fā)現(xiàn)有500G,靠系統(tǒng)的復(fù)制功能怕是得好幾個(gè)小時(shí),于是回來(lái)學(xué)一手操作,話不多說(shuō)上代碼:
說(shuō)明:CopyFiles1是可以將sourceDir連子目錄一起原樣復(fù)制到targetDir,而CopyFiles2是在sourceDir中篩選特定格式文件,然后將其直接放在targetDir中,會(huì)很亂,但是很快
import os
import time
import shutil
sourceDir = r"D:\copytest\datatest"
targetDir = r"D:\copytest\result"
copyFileCounts = 0
def CopyFiles1(sourceDir, targetDir):
#完全連子目錄也會(huì)復(fù)制好,美觀
global copyFileCounts
print(sourceDir )
print("%s 當(dāng)前處理文件夾%s已處理%s 個(gè)文件" %(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))):
open(targetF, "wb").write(open(sourceF, "rb").read())
print ("%s %s 復(fù)制完畢" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF))
else:
print ("%s %s 已存在,不重復(fù)復(fù)制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF))
if os.path.isdir(sourceF):
copyFiles(sourceF, targetF)
def CopyFiles2(dir):
#會(huì)將目錄下所有文件都復(fù)制在一起,速度快,可以篩選文件
i=0
for root,dir1,filename in os.walk(dir):
#print(filename)
for index in range(len(filename)):
#print(os.path.splitext(filename[index])[1])
#if os.path.splitext(filename[index])[1]=='.':#這里注意filename是個(gè)元組,splitext方法的時(shí)候只能是字符串
if 1==1:
#i+=1
print('here')
root1="D:\\copytest\\result3"
old_path = os.path.join(root, filename[index])
print(old_path)
new_path = os.path.join(root1,filename[index])
shutil.copyfile(old_path,new_path)
#print("總共有",i,"圖層文件被復(fù)制!")
if __name__ == "__main__":
time_start = time.time()
try:
import psyco
psyco.profile()
except ImportError:
pass
#CopyFiles1(sourceDir,targetDir)
CopyFiles2("D:/copytest/datatest")
time_end = time.time()
print('totally cost', time_end - time_start)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python的shutil模塊中文件的復(fù)制操作函數(shù)詳解
- Python復(fù)制文件操作實(shí)例詳解
- python復(fù)制文件代碼實(shí)現(xiàn)
- python復(fù)制文件的方法實(shí)例詳解
- 淺談Python實(shí)現(xiàn)2種文件復(fù)制的方法
- python通過(guò)shutil實(shí)現(xiàn)快速文件復(fù)制的方法
- python復(fù)制文件到指定目錄的實(shí)例
- python批量復(fù)制圖片到另一個(gè)文件夾
- Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法
- python調(diào)用cmd復(fù)制文件代碼分享
相關(guān)文章
Python實(shí)現(xiàn)的tcp端口檢測(cè)操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)的tcp端口檢測(cè)操作,結(jié)合實(shí)例形式分析了Python使用socket模塊實(shí)現(xiàn)tcp端口檢測(cè)功能的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
python切片復(fù)制列表的知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python切片復(fù)制列表的知識(shí)點(diǎn)相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10
Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解
這篇文章主要介紹了Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python中POST調(diào)用Restful接口示例
這篇文章主要介紹了Python之POST調(diào)用Restful接口示例,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02

