最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python檢測文件夾變化,并拷貝有更新的文件到對應(yīng)目錄的方法

 更新時間:2018年10月17日 09:25:24   作者:fuyou_h  
今天小編就為大家分享一篇python檢測文件夾變化,并拷貝有更新的文件到對應(yīng)目錄的方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

檢測文件夾,拷貝有更新的文件到對應(yīng)目錄 2016.5.19

親測可用,若有借鑒請修改下文件路徑;

學習python小一個月后寫的這個功能,屬于初學,若有大神路過,求代碼優(yōu)化~

newcopy.py:

檢測文件夾中最后修改時間變化的文件,并拷貝復(fù)制到相應(yīng)路徑下,拷貝目錄會自動檢測后輸出;測試文件夾路徑記得修改;

pyinotify.py:

借用window接口,檢測腳本所在目錄下文件夾變化(更新、刪除、添加等),輸出日志到桌面上;

# newcopy.py文件
# -*- coding:UTF-8 -*-
import os
import os.path
import sys
import time
import datetime
import stat
import difflib
import linecache, shutil

# 文件全路徑和對應(yīng)最后修改時間寫入到out.txt文檔中;
def add_log(path):
 with open('out.txt','w') as f:
  f.close()
 for root , dirs, files in os.walk(path):
  for name in files:
   temp_path = os.path.join(root,name)
   file_name = temp_path.replace('C:/Users/Enter/Desktop/', '')
   file_time = os.stat(temp_path).st_mtime
   with open('out.txt','a') as f:
    f.write( ','.join( ['%s' % file_name , '%s\n' % file_time] ) )
    f.close()

 # 注意時間格式轉(zhuǎn)換
   #file_time = time.localtime(os.stat(root).st_mtime)
   #file_time=date.strftime('%Y-%m-%d %H:%M:%S')

def if_exist():

 # 判斷文件out.txt是否存在,不存在則創(chuàng)建
 filename = 'out.txt'
 if os.path.exists(filename):
  message = 'OK, the "%s" file exists.'
 else:
  message = "Sorry, I cannot find the '%s' file..and I create it."
  a = open('out.txt', 'w')
  a.close()
 print message % filename

 # 判斷update文件夾是否存在,不存在則創(chuàng)建
 files_name='update'
 if os.path.exists(files_name):
  message = 'OK, the "%s" file exists.'
 else:
  message = "Sorry, I cannot find the '%s' file.and I create it. "
  os.mkdir('update')
 print message % files_name


# path 待比較的文件夾路徑
# 返回生成的txt(包含更新或者添加的文件路徑)的路徑
def log_compare(path):

 # 先確保out.txt存在
 if_exist()

 # 獲取out.txt文件內(nèi)容(文件全路徑key和最后修改時間value),生成dict
 txt = open('out.txt', 'r').readlines()
 myDic = {}
 for row in txt:
  (key, value) = row.split(',')
  myDic[key] = value
 print myDic

 # 創(chuàng)建以時間命名的文件和文件夾
 setup_filename = str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))    # 獲取當前時間
 setup_file_path = '%s%s.txt' %('C:/Users/Enter/Desktop/update/' ,setup_filename) # 生成以當前時間命名的.txt文件,準備寫入更新日志
 setup_file_dir = '%s%s' %('C:/Users/Enter/Desktop/update/' ,setup_filename)  # 生成以當前時間命名的.txt文件夾

 #判斷key,比較value值是否變化
 #原始需要有一個out.txt文件,才能比較value確定是否有更新
 #運行程序時,重新遍歷一遍文件全路徑和最后修改時間
 for root , dirs, files in os.walk(path):
  for name in files:
   temp_path = os.path.join(root,name)
   file_name = temp_path.replace('C:/Users/Enter/Desktop/', '')
   time = os.stat(temp_path).st_mtime        # 獲取最后修改時間
   file_time = '%s\n' % time          # 加%s\n是為了與out.txt里值完全對應(yīng)
   if myDic.has_key(file_name) == True:
    if cmp(myDic[file_name], file_time):  # myDic[file_name]舊最后修改時間,file_time新最后修改時間
     print (file_name,file_time)    # 輸出有變化的文件名及其對應(yīng)的最后修改時間

     # 輸出以文件時間命名的更新日志,生成路徑是update下
     with open(setup_file_path,'a') as f: # 有更新的文件,寫入更新日志
      f.write( '%s\n' % file_name )
      f.close()
   else:
    print "add",file_name
    with open(setup_file_path,'a') as f:   # 新增的文件,寫入更新日志
      f.write( '%s\n' % file_name )
      f.close()

  # 返回 當前時間,以時間命名的文件夾路徑,更新文件路徑
 return (setup_filename, setup_file_dir, setup_file_path)

# 將src目錄中的內(nèi)容拷貝到dest目錄
# 如果dest或者其子目錄不存在,先創(chuàng)建
# txt_path為更新日志路徑,有更新的文件才拷貝
def copy_directory(src, dest, txt_path):
 if not os.path.exists(txt_path):
  print "no file update"
  return

 # 讀更新日志,獲取更新文件的全路徑

 txt = open(txt_path, 'r').readlines()
 myDic = {}
 myDic2 = {}
 for row in txt:
  myDic[row] = "1"
  tempArray = os.path.split(row)
  key = tempArray[0]
  myDic2[key] = "1"

 print "myDic2:", myDic2
 print "dict:", myDic

 # 遍歷原始文件夾,得到所有文件的全路徑
 for root, dirs, files in os.walk(src):
  for name in files:
   #print "dirs:",dirs
   fpath = os.path.join(root, name)
   newroot = root
   newroot = newroot.replace(src, dest)  # 根據(jù)文件絕對路徑,創(chuàng)建將要拷貝的路徑(相對路徑),沒有則創(chuàng)建
   #print newroot
   rel_dir = root.replace('C:/Users/Enter/Desktop/', '')
   if not os.path.exists(newroot) and myDic2.has_key(rel_dir):
    print "rel_dir:" , rel_dir
    print newroot
    os.makedirs(newroot)
    os.chmod(newroot, stat.S_IWRITE)
   temp = fpath
   temp = temp.replace(src, dest)
   rel_path = fpath.replace('C:/Users/Enter/Desktop/', '')  # 將絕對路徑改為相對路徑,便于遍歷對比,挑出要拷貝的文件
   rel_path += '\n'

   if myDic.has_key(rel_path) == True:
    print "real_path:" , rel_path
    # os.mkdir(rel_path)
    shutil.copy(fpath, temp)
    print "copyfile:", fpath


def main():

 path_dir = 'C:/Users/Enter/Desktop/acd'
 path_file = 'C:/Users/Enter/Desktop/out.txt'

 params = log_compare(path_dir)
 add_log(path_dir)
 copy_directory(path_dir, params[1], params[2])


if __name__ == '__main__':
 main()
#pyinotify.py文件
# -*- coding:UTF-8 -*-
import os
import win32file
import win32con
# #檢測當前目錄下所有文件刪除、更新、修改等變化。更新日志輸出到桌面。2016.5.23 copy


ACTIONS = {
 1 : "Created",
 2 : "Deleted",
 3 : "Updated",
 4 : "Renamed from something",
 5 : "Renamed to something"
}
# Thanks to Claudio Grondi for the correct set of numbers
FILE_LIST_DIRECTORY = 0x0001
path_to_watch = "."
hDir = win32file.CreateFile (
 path_to_watch,
 FILE_LIST_DIRECTORY,
 win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
 None,
 win32con.OPEN_EXISTING,
 win32con.FILE_FLAG_BACKUP_SEMANTICS,
 None
)
while 1:
 #
 # ReadDirectoryChangesW takes a previously-created
 # handle to a directory, a buffer size for results,
 # a flag to indicate whether to watch subtrees and
 # a filter of what changes to notify.
 #
 # NB Tim Juchcinski reports that he needed to up
 # the buffer size to be sure of picking up all
 # events when a large number of files were
 # deleted at once.
 #
 results = win32file.ReadDirectoryChangesW (
 hDir,
 1024,
 True,
  win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
  win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
  win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
  win32con.FILE_NOTIFY_CHANGE_SIZE |
  win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
  win32con.FILE_NOTIFY_CHANGE_SECURITY,
 None,
 None
 )

 #print "results:", results

 for action, file in results:
 full_filename = os.path.join (path_to_watch, file)
 print full_filename, ACTIONS.get (action, "Unknown")
 with open('C:/Users/Enter/Desktop/fileupdate.txt','a') as f:
  #str = ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] )
  #print str
  f.write( ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] ) )
  f.close()

以上這篇python檢測文件夾變化,并拷貝有更新的文件到對應(yīng)目錄的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一文教你如何在Python中忽略煩人的警告

    一文教你如何在Python中忽略煩人的警告

    當你用 Python 寫代碼時,有時候會看到一些“警告”信息,這些信息不會讓代碼出錯,但會讓輸出看起來很亂,所以本文為大家整理了一些忽略警告的方法,希望對大家有所幫助
    2024-12-12
  • Django單元測試中Fixtures的使用方法

    Django單元測試中Fixtures的使用方法

    這篇文章主要介紹了Django單元測試中Fixtures用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • python 多進程隊列數(shù)據(jù)處理詳解

    python 多進程隊列數(shù)據(jù)處理詳解

    今天小編就為大家分享一篇python 多進程隊列數(shù)據(jù)處理詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python中除法使用的注意事項

    Python中除法使用的注意事項

    這篇文章主要介紹了Python中除法使用的注意事項,是Python程序設(shè)計很重要的技巧,需要的朋友可以參考下
    2014-08-08
  • Python常見工廠函數(shù)用法示例

    Python常見工廠函數(shù)用法示例

    這篇文章主要介紹了Python常見工廠函數(shù)用法,簡單描述了工廠函數(shù)的功能、定義并結(jié)合具體實例形式分析了Python常見工廠函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下
    2018-03-03
  • python中刪除某個元素的方法解析

    python中刪除某個元素的方法解析

    這篇文章主要介紹了python中刪除某個元素的方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • Python開發(fā)入門之如何制作一個簡單的桌面應(yīng)用

    Python開發(fā)入門之如何制作一個簡單的桌面應(yīng)用

    這篇文章主要給大家介紹了關(guān)于Python開發(fā)入門之如何制作一個簡單的桌面應(yīng)用的相關(guān)資料,我們不僅可以使用Python的圖像處理庫,如PIL等來實現(xiàn)圖片的處理和識別,同時你還可以設(shè)計和開發(fā)具有圖形界面的桌面應(yīng)用程序,需要的朋友可以參考下
    2023-08-08
  • 解決Numpy中sum函數(shù)求和結(jié)果維度的問題

    解決Numpy中sum函數(shù)求和結(jié)果維度的問題

    今天小編大家分享一篇解決Numpy中sum函數(shù)求和結(jié)果維度的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Django 實現(xiàn)前端圖片壓縮功能的方法

    Django 實現(xiàn)前端圖片壓縮功能的方法

    今天小編就為大家分享一篇Django 實現(xiàn)前端圖片壓縮功能的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 一文教你如何用Python輕輕松松操作Excel,Word,CSV

    一文教你如何用Python輕輕松松操作Excel,Word,CSV

    數(shù)據(jù)處理是 Python 的一大應(yīng)用場景,而 Excel 又是當前最流行的數(shù)據(jù)處理軟件。本文將為大家詳細介紹一下如何用Python輕輕松松操作Excel、Word、CSV,需要的可以參考一下
    2022-02-02

最新評論

游戏| 临猗县| 遂昌县| 横峰县| 皋兰县| 绥中县| 弥渡县| 新安县| 平定县| 广丰县| 金门县| 郯城县| 开化县| 鸡西市| 汉寿县| 区。| 文水县| 阳谷县| 茶陵县| 太仓市| 惠东县| 安岳县| 邵阳市| 象山县| 和田县| 红安县| 正蓝旗| 铜山县| 洛浦县| 定兴县| 曲麻莱县| 汨罗市| 兴宁市| 金堂县| 磐石市| 开化县| 绍兴县| 吉隆县| 安吉县| 白银市| 元江|