我想寫一個python腳本,將某些文件從源文件夾複製到目標,但只應複製新創建的文件。 例如,源文件夾包含2005文件,目標文件夾包含2000個文件,我的腳本應該複製5個丟失的文件Python - 如何只複製新的修改後的文件
在下面的腳本中,邏輯是記錄擴展名爲「 。擴展名」,並試圖得到它,它在下一步
import sys, os, shutil
import glob
import os.path, time
fob = open(r"C:\Python\Log.txt","a")
dir_src = r"C:\Python\Source"
dir_dst = r"C:\Python\Dest"
for w in os.listdir(dir_src):
if w.endswith('.extenstion'):
pathname = os.path.join(dir_src, w)
if not Date_File in (fob):
shutil.copy2(pathname, dir_dst)
fob.write("File Name: %s" % os.path.basename(pathname))
fob.write(" Last modified Date: %s" % time.ctime(os.path.getmtime(pathname)))
fob.write(" Copied On: %s" % time.strftime("%c"))
fob.write("\n")
fob.close()
os.system("PAUSE")
比較我無法找到一個方法來比較的日期和檢查是一個文件應該被複制或沒有。
歡迎任何其他方法。
看看['filecmp'](https://docs.python.org/2/library/filecmp.html) – 2015-02-24 14:01:21
爲什麼日期很重要? – 2015-02-24 14:05:56