f29825604 發表於 19-7-30 10:55

python:watch_file,copy,rename

本帖最後由 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:


合併兩個檔案


#!python3
import os
import sys
import time
import shutil
import datetime

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

class Watcher(object):
    running = True
    refresh_delay_secs = 1

    # Constructor
    def __init__(self, watch_file, call_func_on_change=None, *args, **kwargs):
      self._cached_stamp = 0
      self.filename = watch_file
      self.call_func_on_change = call_func_on_change
      self.args = args
      self.kwargs = kwargs

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

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

    # Keep watching in a loop      
    def watch(self):
      while self.running:
            try:
                # Look for changes
                time.sleep(self.refresh_delay_secs)
                self.look()
            except KeyboardInterrupt:
                print('\nDone')
                break
            except FileNotFoundError:
                # Action on file not found
                pass
            except:
                print('Unhandled error: %s' % sys.exc_info())

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

watch_file = 'my_file.txt'

# watcher = Watcher(watch_file)# simple
watcher = Watcher(watch_file, custom_action, text='yes, changed')# also call custom action function
watcher.watch()# start the watch going

























f29825604 發表於 19-8-19 19:56

本帖最後由 f29825604 於 19-8-19 20:06 編輯

#監看d:\temp下檔案的變化事件#移動,新增,刪除,修正#from watchdog.observers import Observer
from watchdog.events import *
import time

class FileEventHandler(FileSystemEventHandler):
    def __init__(self):
      FileSystemEventHandler.__init__(self)

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

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

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

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

if __name__ == "__main__":
    observer = Observer()
    event_handler = FileEventHandler()
    observer.schedule(event_handler,"d:/temp",True)
    observer.start()
    try:
      while True:
            time.sleep(1)
    except KeyboardInterrupt:
      observer.stop()
    observer.join()

f29825604 發表於 21-1-10 16:00

本帖最後由 f29825604 於 21-1-10 16:04 編輯

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


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

感謝 生魚片交易訊號應該也可以試試看
頁: [1]
查看完整版本: python:watch_file,copy,rename