logo
down
shadow

Python watchdog, watch a directory and rename file on event.modification


Python watchdog, watch a directory and rename file on event.modification

By : reb93
Date : January 11 2021, 03:32 PM
wish help you to fix your issue I have found that on_modified events can 'fire' twice for the same file. I solved this with a deque of recent events for my application, but a last_event variable would work to prevent duplicates. You may want to find a way of re-setting last_event, if you are expecting input files with the same name.
Also I have found a short wait in on_modified() to allow the file to be fully written to be very useful in preventing unexpected behavior if performing operations on that file.
code :
last_event = ''

def on_modified(self, event):
    time.sleep(1)  # wait to allow file to be fully written
    if not event.src_path == last_event:  # files we haven't seen recently
         # do something
         last_event = event.src_path
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
    print(f'event type: {event.event_type}  path : {event.src_path}')
    try:
        df = pd.read_csv(event.src_path) # read the file
        course = df['Course Name'].unique().tolist()[0] # pass course name to a variable
        copyfile(event.src_path, f"{course}.csv") # copy file, using os.rename threw up an error.
        os.remove(event.src_path) # remove original file.
        print("file renamed")
    except FileNotFoundError:
        # handle the error
        pass


Share : facebook icon twitter icon
Running watchdog file system watch within pyqt

Running watchdog file system watch within pyqt


By : wtfwhereismylife
Date : March 29 2020, 07:55 AM
around this issue The class watchdog.observers.Observer inherits from threading.Thread, so the actual checking is done in a thread that you start when you call observer.start().
The loop doesn't do anything except letting the program run until the user interrupts it. You can remove it completely and run the Qt event loop as usual:
code :
observer.start()
status = app.exec_()
observer.stop()
observer.join()
sys.exit(status)
python watchdog monitoring directory for created files and doing something to that file

python watchdog monitoring directory for created files and doing something to that file


By : Abdo Shaaban
Date : March 29 2020, 07:55 AM
Hope this helps I think you might need to add a sleep right after the file is found. I think you script is reading it, before it has actual data in the pipe.
Using python-watchdog to monitor a folder, but when I rename a file, I haven't been able to find a way to see the new fi

Using python-watchdog to monitor a folder, but when I rename a file, I haven't been able to find a way to see the new fi


By : illflux
Date : March 29 2020, 07:55 AM
Hope this helps if you don't know what are the methods and properties an object have just do print dir(obj) here in your case event.dest_path will do the work
How do I watch a file, not a directory for changes using Python?

How do I watch a file, not a directory for changes using Python?


By : user3718970
Date : March 29 2020, 07:55 AM
it should still fix some issue You can watch a file with watchdog by watching the directory that the file is in and only responding to change events that effect your file. Something like this would do it for you:
code :
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class FileModifiedHandler(FileSystemEventHandler):

    def __init__(self, path, file_name, callback):
        self.file_name = file_name
        self.callback = callback

        # set observer to watch for changes in the directory
        self.observer = Observer()
        self.observer.schedule(self, path, recursive=False)
        self.observer.start()
        self.observer.join()

    def on_modified(self, event): 
        # only act on the change that we're looking for
        if not event.is_directory and event.src_path.endswith(self.file_name):
            self.observer.stop() # stop watching
            self.callback() # call callback


from sys import argv, exit

if __name__ == '__main__':

    if not len(argv) == 2:
        print("No file specified")
        exit(1)

    def callback():
        print("FILE WAS MODIFED")

    FileModifiedHandler('.', argv[1], callback)
File watch in the directory using Python and then send data using POST request on file modification

File watch in the directory using Python and then send data using POST request on file modification


By : user3186676
Date : March 29 2020, 07:55 AM
Hope this helps Problem 1: Event "modified" fired twice
This issue appears because multiple operations can occur when you save a file, data are changed, then the metadata (last modified ...). It can be hard to handle depending on what you need and the frequency of changes if there are many users.
code :
import os
from collections import defaultdict

class Handler(FileSystemEventHandler):

    def __init__(self):
        super().__init__()
        # this dict will store the filenames and the time
        self.files = defaultdict(lambda:0)

    def on_any_event(self, event):
        if event.is_directory:
            return None

        elif (event.event_type == 'modified' and 
              event.src_path.endswith(('.xls','.xlsx'))) :

            # here we get the last change timestamp 
            stats = os.stat(event.src_path).st_mtime

            # delay of 1 sec minimum between 2 changes
            # self.files[event.src_path] is set to 0 thanks to the defaultdict
            if stats - self.files[event.src_path] > 1: 
                print('api call with ', event.src_path)
                # do what you need

            # then update the time for this file
            self.files[event.src_path] = stats
class Handler(FileSystemEventHandler):

    def __init__(self, path):
        super().__init__()
        self.path = path        
def start(self):
    event_handler = Handler(self.directory) # or Handler() if you remove path
    self.observer.schedule(event_handler, self.directory, recursive=True)
    self.observer.start()
Related Posts Related Posts :
  • Tuning the hyperparameter with gridsearch results in overfitting
  • some coordinates that I extracted from geocoder in Python are not saving in the variable I created
  • 7C in cs circles- python Im not sure what is wrong with this yet
  • How to fix 'AttributeError: 'list' object has no attribute 'shape'' error in python with Tensorflow / Keras when loading
  • python - thread`s target is a method of an object
  • Retrieve Variable From Class
  • What is the reason for matplotlib for printing labels multiple times?
  • Why would people use ThreadPoolExecutor instead of direct function call?
  • When clear_widgets is called, it doesnt remove screens in ScreenManager
  • Python can't import function
  • Pieces doesn't stack after one loop on my connect4
  • How to change font size of all .docx document with python-docx
  • How to store a word with # in .cfg file
  • How to append dictionaries to a dictionary?
  • How can I scrape text within paragraph tag with some other tags then within the paragraph text?
  • Custom entity ruler with SpaCy did not return a match
  • Logging with two handlers - one to file and one to stderr
  • How to do pivot_table in dask with aggfunc 'min'?
  • This for loop displays only the last entry of the student record
  • How to split a string by a specific pattern in number of characters?
  • Python 3: how to scrape research results from a website using CSFR?
  • Setting the scoring parameter of RandomizedSeachCV to r2
  • How to send alert or message from view.py to template?
  • How to add qml ScatterSeries to existing qml defined ChartView?
  • Django + tox: Apps aren't loaded yet
  • My css and images arent showing in django
  • Probability mass function sum 2 dice roll?
  • Cannot call ubuntu 'ulimit' from python subprocess without using shell option
  • Dataframe Timestamp Filter for new/repeating value
  • Problem with clicking select2 dropdownlist in selenium
  • pandas dataframe masks to write values into new column
  • How to click on item in navigation bar on top of page using selenium python?
  • Add multiple EntityRuler with spaCy (ValueError: 'entity_ruler' already exists in pipeline)
  • error when replacing missing ')' using negative look ahead regex in python
  • Is there a way to remove specific strings from indexes using a for loop?
  • select multiple tags by position in beautifulSoup
  • pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout
  • Shipping PyGObject/GTK+ app on Windows with MingW
  • Python script to deduplicate lines in multiple files
  • How to prevent window and widgets in a pyqt5 application from changing size when the visibility of one widget is altered
  • How to draw stacked bar plot from df.groupby('feature')['label'].value_counts()
  • Python subprocess doesn't work without sleep
  • How can I adjust 'the time' in python with module Re
  • Join original np array with resulting np array in a form of dictionary? multidimensional array? etc?
  • Forcing labels on histograms in each individual graph in a figure
  • For an infinite dataset, is the data used in each epoch the same?
  • Is there a more efficent way to extend a string?
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org