python復(fù)制文件代碼實現(xiàn)
主要功能在copyFiles()函數(shù)里實現(xiàn),如下:
def copyFiles(src, dst):
srcFiles = os.listdir(src)
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
filesCopiedNum = 0
# 對源文件夾中的每個文件若不存在于目的文件夾則復(fù)制
for file in srcFiles:
src_path = os.path.join(src, file)
dst_path = os.path.join(dst, file)
# 若源路徑為文件夾,若存在于目標(biāo)文件夾,則遞歸調(diào)用本函數(shù);否則先創(chuàng)建再遞歸。
if os.path.isdir(src_path):
if not os.path.isdir(dst_path):
os.makedirs(dst_path)
filesCopiedNum += copyFiles(src_path, dst_path)
# 若源路徑為文件,不重復(fù)則復(fù)制,否則無操作。
elif os.path.isfile(src_path):
if not dstFiles.has_key(file):
shutil.copyfile(src_path, dst_path)
filesCopiedNum += 1
return filesCopiedNum
這里我首先使用os.listdir()函數(shù)來遍歷源文件夾src和目標(biāo)文件夾dst,得到兩個文件列表,但由于我需要判重操作,因此需要在dst文件列表中進(jìn)行查詢操作。由于列表的查詢效率不高,而字典是一個哈希表,查詢效率較高,因此我將目標(biāo)文件列表轉(zhuǎn)換成一個只有鍵沒有值的字典:
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
然后我遍歷源文件列表,若該路徑是一個文件夾,先判斷該文件夾在目標(biāo)路徑中是否存在,若不存在,則先創(chuàng)建一個新路徑。然后遞歸調(diào)用本函數(shù)。其實不存在的時候更高效的方法是調(diào)用shutil.copytree()函數(shù),但由于此處需要計算拷貝的文件數(shù)量,因此就沒有調(diào)用該函數(shù)。
若該路徑是一個文件,則首先判斷該文件在目標(biāo)文件夾中是否存在。若不存在,則拷貝。
由于寫這個腳本主要是為了同步手機相冊到PC,因此只簡單地判斷一下文件名。若要判斷不同名但相同的文件,則可以繼續(xù)判斷一下md5值,這里就不再贅述。
完整代碼如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 輸入兩個文件夾a和b路徑,將a中的文件拷進(jìn)b,并計算拷貝的文件數(shù)。重復(fù)的不作處理。
import os
import shutil
def copyFiles(src, dst):
srcFiles = os.listdir(src)
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
filesCopiedNum = 0
# 對源文件夾中的每個文件若不存在于目的文件夾則復(fù)制
for file in srcFiles:
src_path = os.path.join(src, file)
dst_path = os.path.join(dst, file)
# 若源路徑為文件夾,若存在于目標(biāo)文件夾,則遞歸調(diào)用本函數(shù);否則先創(chuàng)建再遞歸。
if os.path.isdir(src_path):
if not os.path.isdir(dst_path):
os.makedirs(dst_path)
filesCopiedNum += copyFiles(src_path, dst_path)
# 若源路徑為文件,不重復(fù)則復(fù)制,否則無操作。
elif os.path.isfile(src_path):
if not dstFiles.has_key(file):
shutil.copyfile(src_path, dst_path)
filesCopiedNum += 1
return filesCopiedNum
def test():
src_dir = os.path.abspath(raw_input('Please enter the source path: '))
if not os.path.isdir(src_dir):
print 'Error: source folder does not exist!'
return 0
dst_dir = os.path.abspath(raw_input('Please enter the destination path: '))
if os.path.isdir(dst_dir):
num = copyFiles(src_dir, dst_dir)
else:
print 'Destination folder does not exist, a new one will be created.'
os.makedirs(dst_dir)
num = copyFiles(src_dir, dst_dir)
print 'Copy complete:', num, 'files copied.'
if __name__ == '__main__':
test()
相關(guān)文章
解析pandas apply() 函數(shù)用法(推薦)
這篇文章主要介紹了pandas apply() 函數(shù)用法,大家需要掌握函數(shù)作為一個對象,能作為參數(shù)傳遞給其它函數(shù),也能作為函數(shù)的返回值,具體內(nèi)容詳情跟隨小編一起看看吧2021-10-10Python+OpenCV檢測燈光亮點的實現(xiàn)方法
這篇文章主要介紹了Python+OpenCV檢測燈光亮點的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11基于Python+Turtle實現(xiàn)繪制簡易的大風(fēng)車
大風(fēng)車,吱呀吱呦呦地轉(zhuǎn),這里的風(fēng)景呀真好看!天好看,地好看……一首熟悉的歌曲,是否已經(jīng)把你拉回了童年?本文將用Turtle庫繪制簡易的大風(fēng)車,需要的可以參考一下2022-06-06基于Python實現(xiàn)PDF批量轉(zhuǎn)化工具
這篇文章主要為大家詳細(xì)介紹了如何基于Python制作一個PDF批量轉(zhuǎn)化工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12macbook如何徹底刪除python的實現(xiàn)方法
本文主要介紹了macbook如何徹底刪除python的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07