請選擇 進入手機版 | 繼續訪問電腦版

COCO研究院

 找回密碼
 註冊
搜索
查看: 14811|回復: 2

[其他程式語言] python:watch_file,copy,rename

[複製鏈接]
發表於 19-7-30 10:55 | 顯示全部樓層 |閱讀模式
本帖最後由 f29825604 於 19-7-30 11:21 編輯

How do I watch a file for changes?回答17:Python copy files to a new directory and rename if file name already exists
回答:I always use the time-stamp - so its not possible, that the file exists already:


合併兩個檔案


  1. #!python3
  2. import os
  3. import sys
  4. import time
  5. import shutil
  6. import datetime

  7. # 看'my_file.txt'發生變化時,
  8. # 複製檔案copy(src_dir,dst_dir),
  9. # 加上str(now)變化當下的時間重新命名

  10. class Watcher(object):
  11.     running = True
  12.     refresh_delay_secs = 1

  13.     # Constructor
  14.     def __init__(self, watch_file, call_func_on_change=None, *args, **kwargs):
  15.         self._cached_stamp = 0
  16.         self.filename = watch_file
  17.         self.call_func_on_change = call_func_on_change
  18.         self.args = args
  19.         self.kwargs = kwargs

  20.     # Look for changes
  21.     def look(self):
  22.         stamp = os.stat(self.filename).st_mtime
  23.         if stamp != self._cached_stamp:
  24.             self._cached_stamp = stamp
  25.             # File has changed, so do something...
  26.             now = str(datetime.datetime.now())[:19]
  27.             now = now.replace(":","_")

  28.             src_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand.xlsx"
  29.             dst_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand_"+str(now)+".xlsx"
  30.             shutil.copy(src_dir,dst_dir)
  31.             print('File changed'+str(now))
  32.             if self.call_func_on_change is not None:
  33.                 self.call_func_on_change(*self.args, **self.kwargs)

  34.     # Keep watching in a loop        
  35.     def watch(self):
  36.         while self.running:
  37.             try:
  38.                 # Look for changes
  39.                 time.sleep(self.refresh_delay_secs)
  40.                 self.look()
  41.             except KeyboardInterrupt:
  42.                 print('\nDone')
  43.                 break
  44.             except FileNotFoundError:
  45.                 # Action on file not found
  46.                 pass
  47.             except:
  48.                 print('Unhandled error: %s' % sys.exc_info()[0])

  49. # Call this function each time a change happens
  50. def custom_action(text):
  51.     print(text)

  52. watch_file = 'my_file.txt'

  53. # watcher = Watcher(watch_file)  # simple
  54. watcher = Watcher(watch_file, custom_action, text='yes, changed')  # also call custom action function
  55. watcher.watch()  # start the watch going
複製代碼
























複製代碼


 樓主| 發表於 19-8-19 19:56 | 顯示全部樓層
本帖最後由 f29825604 於 19-8-19 20:06 編輯
  1. #監看d:\temp下檔案的變化事件#移動,新增,刪除,修正#
複製代碼
  1. from watchdog.observers import Observer
  2. from watchdog.events import *
  3. import time

  4. class FileEventHandler(FileSystemEventHandler):
  5.     def __init__(self):
  6.         FileSystemEventHandler.__init__(self)

  7.     def on_moved(self, event):
  8.         if event.is_directory:
  9.             print("directory moved from {0} to {1}".format(event.src_path,event.dest_path))
  10.         else:
  11.             print("file moved from {0} to {1}".format(event.src_path,event.dest_path))

  12.     def on_created(self, event):
  13.         if event.is_directory:
  14.             print("directory created:{0}".format(event.src_path))
  15.         else:
  16.             print("file created:{0}".format(event.src_path))

  17.     def on_deleted(self, event):
  18.         if event.is_directory:
  19.             print("directory deleted:{0}".format(event.src_path))
  20.         else:
  21.             print("file deleted:{0}".format(event.src_path))

  22.     def on_modified(self, event):
  23.         if event.is_directory:
  24.             print("directory modified:{0}".format(event.src_path))
  25.         else:
  26.             print("file modified:{0}".format(event.src_path))

  27. if __name__ == "__main__":
  28.     observer = Observer()
  29.     event_handler = FileEventHandler()
  30.     observer.schedule(event_handler,"d:/temp",True)
  31.     observer.start()
  32.     try:
  33.         while True:
  34.             time.sleep(1)
  35.     except KeyboardInterrupt:
  36.         observer.stop()
  37.     observer.join()
複製代碼

 樓主| 發表於 21-1-10 16:00 | 顯示全部樓層
本帖最後由 f29825604 於 21-1-10 16:04 編輯

sdsaeleader with Pandas/Numpy
在eleader程式交易中,除了提供類似easy_language的語法之外,其實內容是可用Python的語法。


Python File Write
Example
Open the file "demofile3.txt" and overwrite the content:


感謝 生魚片
交易訊號應該也可以試試看
2021-01-10 15 38.jpg 2021-01-10 15 36.jpg

2021-01-10 15 38.jpg
2021-01-10 15 36.jpg
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-3-29 22:42

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook