Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6710151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:59:20+00:00 2026-05-26T07:59:20+00:00

Ex, I need to catch remove and add files events on some directory on

  • 0

Ex, I need to catch remove and add files events on some directory on linux os. I found libs like inotify and python wrappers for them, but if I want to use clear python code should I watch for os.listdir(path) output every sec or are there some ways to accomplish such task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:59:21+00:00Added an answer on May 26, 2026 at 7:59 am

    Source: http://code.activestate.com/recipes/215418-watching-a-directory-tree-on-unix/

    The watch_directories() function takes a list of paths and a callable object, and then repeatedly traverses the directory trees rooted at those paths, watching for files that get deleted or have their modification time changed. The callable object is then passed two lists containing the files that have changed and the files that have been removed.

    from __future__ import nested_scopes
    
    import os, time
    
    def watch_directories (paths, func, delay=1.0):
        """(paths:[str], func:callable, delay:float)
        Continuously monitors the paths and their subdirectories
        for changes.  If any files or directories are modified,
        the callable 'func' is called with a list of the modified paths of both
        files and directories.  'func' can return a Boolean value
        for rescanning; if it returns True, the directory tree will be
        rescanned without calling func() for any found changes.
        (This is so func() can write changes into the tree and prevent itself
        from being immediately called again.)
        """
    
        # Basic principle: all_files is a dictionary mapping paths to
        # modification times.  We repeatedly crawl through the directory
        # tree rooted at 'path', doing a stat() on each file and comparing
        # the modification time.  
    
        all_files = {}
        def f (unused, dirname, files):
            # Traversal function for directories
            for filename in files:
                path = os.path.join(dirname, filename)
    
                try:
                    t = os.stat(path)
                except os.error:
                    # If a file has been deleted between os.path.walk()
                    # scanning the directory and now, we'll get an
                    # os.error here.  Just ignore it -- we'll report
                    # the deletion on the next pass through the main loop.
                    continue
    
                mtime = remaining_files.get(path)
                if mtime is not None:
                    # Record this file as having been seen
                    del remaining_files[path]
                    # File's mtime has been changed since we last looked at it.
                    if t.st_mtime > mtime:
                        changed_list.append(path)
                else:
                    # No recorded modification time, so it must be
                    # a brand new file.
                    changed_list.append(path)
    
                # Record current mtime of file.
                all_files[path] = t.st_mtime
    
        # Main loop
        rescan = False
        while True:
            changed_list = []
            remaining_files = all_files.copy()
            all_files = {}
            for path in paths:
                os.path.walk(path, f, None)
            removed_list = remaining_files.keys()
            if rescan:
                rescan = False
            elif changed_list or removed_list:
                rescan = func(changed_list, removed_list)
    
            time.sleep(delay)
    
    if __name__ == '__main__':
        def f (changed_files, removed_files):
            print changed_files
            print 'Removed', removed_files
    
        watch_directories(['.'], f, 1)
    

    This recipe is useful where you’d like some way to send jobs to a daemon, but don’t want to use some IPC mechanism such as sockets or pipes. Instead, the daemon can sit and watch a submission directory, and jobs can be submitted by dropping a file or directory into the submission directory.

    Locking is not taken into account. The watch_directories() function itself doesn’t really need to do locking; if it misses a modification on one pass, it’ll notice it on the next pass. However, if jobs are written directly into a watched directory, the callable object might start running while a job file is only half-written. To solve this, you can use a lockfile; the callable must acquire the lock when it runs, and submitters must acquire the lock when they wish to add a new job. A simpler approach is to rely on the rename() system call being atomic: write the job into a temporary directory that isn’t being watched, and once the file is complete use os.rename() to move it into the submission directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a delegate function that can 'wrap' some while/try/catch code around
VB 6.0 does not have any global handler.To catch runtime errors,we need to add
Is this good enough ? do I need to add anything or remove anything
I need to catch the HTML of a ASP.NET just before it is being
I need to catch segmentation fault in third party library cleanup operations. This happens
I need to catch that exception but I can't figure out which one it
I need to catch two different swipping gestures using UISwipeGestureRecognizer (for example, UISwipeGestureRecognizerDirectionRight and
I have gtk.Window and I need to catch closure. I need to close the
I'm redesigning a class constructor in C++ and need it to catch an unspecified
I need a little help creating a catch-all error handling page in my ICEfaces

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.