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 883677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:37:53+00:00 2026-05-15T12:37:53+00:00

Take the following demo code (from the GIO answer to this question), which uses

  • 0

Take the following demo code (from the GIO answer to this question), which uses a GIO FileMonitor to monitor a directory for changes:

import gio

def directory_changed(monitor, file1, file2, evt_type):
    print "Changed:", file1, file2, evt_type

gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed) 

import glib
ml = glib.MainLoop()
ml.run()

After running this code, I can then create and modify child nodes and be notified of the changes. However, this only works for immediate children (I am aware that the docs don’t say otherwise). The last of the following shell commands will not result in a notification:

touch one
mkdir two
touch two/three

Is there an easy way to make it recursive? I’d rather not manually code something that looks for directory creation and adds a monitor, removing them on deletion, etc.

The intended use is for a VCS file browser extension, to be able to cache the statuses of files in a working copy and update them individually on changes. So there might by anywhere from tens to thousands (or more) directories to monitor. I’d like to just find the root of the working copy and add the file monitor there.

I know about pyinotify, but I’m avoiding it so that this works under non-Linux kernels such as FreeBSD or… others. As far as I’m aware, the GIO FileMonitor uses inotify underneath where available, and I can understand not emphasising the implementation to maintain some degree of abstraction, but it suggested to me that it should be possible.

(In case it matters, I originally posted this on the PyGTK mailing list.)

  • 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-15T12:37:54+00:00Added an answer on May 15, 2026 at 12:37 pm

    I’m not sure if GIO allows you to have more than one monitor at once, but if it does there’s no* reason you can’t do something like this:

    import gio
    import os
    
    def directory_changed(monitor, file1, file2, evt_type):
        if os.path.isdir(file2):    #maybe this needs to be file1?
            add_monitor(file2) 
        print "Changed:", file1, file2, evt_type
    
    def add_monitor(dir):
        gfile = gio.File(dir)
        monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
        monitor.connect("changed", directory_changed) 
    
    add_monitor('.')
    
    import glib
    ml = glib.MainLoop()
    ml.run()
    

    *when I say no reason, there’s the possibility that this could become a resource hog, though with nearly zero knowledge about GIO I couldn’t really say. It’s also entirely possible to roll your own in Python with a few commands (os.listdir among others). It might look something like this

    import time
    import os
    
    class Watcher(object):
        def __init__(self):
            self.dirs = []
            self.snapshots = {}
    
        def add_dir(self, dir):
            self.dirs.append(dir)
    
        def check_for_changes(self, dir):
            snapshot = self.snapshots.get(dir)
            curstate = os.listdir(dir)
            if not snapshot:
                self.snapshots[dir] = curstate
            else:
                if not snapshot == curstate:
                    print 'Changes: ',
                    for change in set(curstate).symmetric_difference(set(snapshot)):
                        if os.path.isdir(change):
                            print "isdir"
                            self.add_dir(change)
                        print change,
    
                    self.snapshots[dir] = curstate
                    print
    
        def mainloop(self):
            if len(self.dirs) < 1:
                print "ERROR: Please add a directory with add_dir()"
                return
    
            while True:
                for dir in self.dirs:
                    self.check_for_changes(dir)
                time.sleep(4) # Don't want to be a resource hog
    
    w = Watcher()
    w.add_dir('.')
    
    
    w.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following method (written in Ruby but this question could be applied to
Take the following code: ImageView imageView = new ImageView(activity); imageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); Running this
Take the following code example: File package1/__init__.py : from moduleB import foo print moduleB.__name__
Take the following code snippet: SPAttachmentCollection attachments = item.Attachments ; What exactly is SPAttachmentCollection
Take the following example plugin: (function($) { $.fn.alertOnClick = function(text) { return this.each(function(){ $(this).click(alert(text));
Take the following code: <abbr title=World Health Organization>WHO</abbr> Can we style an abbr tag's
I was following this tutorial: http://www.marcofolio.net/webdesign/a_fancy_apple.com-style_search_suggestion.html And checking out the demo here: http://qpoit.com/marcofolio_demo/apple_search/ I
Take the following code; void DoThrow( const std::exception& e ) { throw e; }
Take the following pseudo C# code: using System; using System.Data; using System.Linq; using System.Collections.Generic;
Take the following controller action public ActionResult NextBySURNAME(int id, string data) { //code to

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.