473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

monitoring folder in python

Is it possible to monitor a folder in the python?My question is if I
put any file in it that particular folder my script should monitor the
folder and read the file name.If so what function can I use?

Thanx in advance

Jul 18 '05 #1
2 7313
Raghul wrote:
Is it possible to monitor a folder in the python?My question is if I
put any file in it that particular folder my script should monitor the
folder and read the file name.If so what function can I use?

Thanx in advance


If you do not want to poll (check for changes yourself regularly),
here are some pointers:

In Windows:

With the win32 extensions, it's quite simple:

http://tgolden.sc.sabren.com/python/...r_changes.html

In Linux:

http://www.edoceo.com/creo/inotify/

http://www.student.lu.se/~nbi98oli/dnotify.html

http://www.lambda-computing.com/projects/dnotify/

Don't know of any Python extensions for the above, so you might
have to write your own if you want to use them from Python. Also,
you probably need to reconfigure and compile a new kernel. Have
tested inotify myself and it seems to work, you also get the filename
with the event. Only problem is it don't support monitoring a directory
recursive (subdirectories) like in Windows with ReadDirectoryChangesW.

Jul 18 '05 #2
A straw-man solution in Python that monitors a directory for changes
(Linux only):

#!/usr/bin/env python2

import sys
import os

if sys.platform not in ("linux2",):
sys.exit("%s only runs on Linux" % os.path.basename(sys.argv[0]))

# TODO: we should check that kernel version >= 2.4.19
# e.g. with 'uname -r'

import fcntl
import signal

TESTDIRECTORY = "."

def notify(directory, handler):
fd = os.open(directory, os.O_RDONLY)
fcntl.fcntl(fd, fcntl.F_NOTIFY,
fcntl.DN_ACCESS|fcntl.DN_MODIFY|fcntl.DN_CREATE)
signal.signal(signal.SIGIO, handler)

def handler(signum, frame):
print "Something happened; signal =", signum

if __name__ == "__main__":
notify(TESTDIRECTORY, handler)
try:
signal.pause() # sleep until signal received
except KeyboardInterrupt:
pass

With Python 2.4 one could bind e.g. to signal.SIGRTMIN instead of the
default SIGIO and get additional info, as suggested by the C example in
Documentation/dnotify.txt (look in the Linux src tree). But then I am
note sure you can access a siginfo structure directly from Python. With
the example above, one would probably write the handler so that it
rescans the directory when called to detect what actually changed.

Davide

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ron Vecchi | last post by:
Hi all, Can someone point me in the direction on this one, resources or examples. I am writing a Windows Service to monitor a folder. When I file is modified within the folder or any subfolders...
0
by: Jeff Reed | last post by:
I am experiencing the the problem outlined the below. Unfortunately, I am using WinXP and I not sure if I can apply the solution due to lack of security control Any feed back would be apreciated ...
7
by: Mark | last post by:
Hello, I have researched and tried every thing I have found on the web, in groups and MS KB articles. Here is what I have. I have a Windows 2000 Domain Controller all service packs and...
4
by: kimpton | last post by:
Hi, My App running on my local machine is trying to access apsx and ascx pages via a virtual directory (see earlier post) that points to a share on a different machine. I can now access .aspx...
2
by: Greg Allen | last post by:
I know this has been discussed before, and have found some documentation about it on the web. But nothing has fixed my problem. I am running the 1.1 .NET framework, SP1. I have a web...
5
by: Joe | last post by:
I'm getting the following error when trying to call a page on a secure server. I'm not doing any impersonations or file access of any kind. The page is using PayPal and I'm wondering if PayPal has...
1
by: amit.vasu | last post by:
Hi I have created a web serivces using .net framework 2.0. When I try to execute the web service I get the following error. Failed to start monitoring changes to 'e:\Default Web Site' because...
1
by: William Connery | last post by:
Hi, I have a small python program with e-mail capabilities that I have pieced together from code snippets found on the internet. The program uses the smtplib module to successfully send an...
5
by: rohit | last post by:
hi, i am designing a desktop search engine using python. i am having a query , is there a package available that contains functions for retrieving the files being edited , created,deleted in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.