I need to monitor a number of folders over a period of time. so i pass the files in each folder to a list of list.

import os

path_to_watch = []
path_to_watch.append("/path1/")
path_to_watch.append("/path2/")
path_to_watch.append("/path3/")

before = [],[]
for item, folder in enumerate(path_to_watch):
    print "i am here", item, " : ", folder
    before[item].append(dict ([(f, None) for f in os.listdir (folder)]))

were path1 to 3 are actual paths on my system

When it reaches the final folder it gives me the error

tuple index out of range

any ideas?

Recommended Answers

All 7 Replies

Does this help you?

Pretty is my module which I posted you can replace it with normal prints and use your paths of course.

import os
import pretty

path_to_watch = []
path_to_watch.append("/Tony/Tests")
path_to_watch.append("/Python Projects/")
path_to_watch.append("/Python26/Lib/site-packages")

before = [],[]
for item, folder in enumerate(path_to_watch):
##    print "i am here", item, " : ", folder
    exp=[(f, None) for f in os.listdir (folder)]
    print 'Before: ',len(before),
    pretty.ppr(before)
    pretty.ppr(item)
    pretty.ppr(before[item])    
##    print item, before[item], len(before)
    if item: before[item].append(dict (exp))
>>> 
Before:  2 
(
  [], 
  [])
0

[]
Before:  2 
(
  [], 
  [])
1

[]
Before:  2 
(
  [], 
  [{'ppitcs_code': None}])
2

Traceback (most recent call last):
  File "D:\Tony\Tests\watchthese.py", line 16, in <module>
    pretty.ppr(before[item])
IndexError: tuple index out of range
>>>

What are you trying to accomplish with this jungle of dicts, Nones and lists?
What expected final result?

Does this help you?

Pretty is my module which I posted you can replace it with normal prints and use your paths of course.

import os
import pretty

path_to_watch = []
path_to_watch.append("/Tony/Tests")
path_to_watch.append("/Python Projects/")
path_to_watch.append("/Python26/Lib/site-packages")

before = [],[]
for item, folder in enumerate(path_to_watch):
##    print "i am here", item, " : ", folder
    exp=[(f, None) for f in os.listdir (folder)]
    print 'Before: ',len(before),
    pretty.ppr(before)
    pretty.ppr(item)
    pretty.ppr(before[item])    
##    print item, before[item], len(before)
    if item: before[item].append(dict (exp))
>>> 
Before:  2 
(
  [], 
  [])
0

[]
Before:  2 
(
  [], 
  [])
1

[]
Before:  2 
(
  [], 
  [{'ppitcs_code': None}])
2

Traceback (most recent call last):
  File "D:\Tony\Tests\watchthese.py", line 16, in <module>
    pretty.ppr(before[item])
IndexError: tuple index out of range
>>>

What are you trying to accomplish with this jungle of dicts, Nones and lists?
What expected final result?

I need to monitor a number of folders for changes, depending on the type of file and folder it will trigger a action.
then second part of the script checks every couple of minutes for changes to the folder structure, i just need lists of all the folders monitored.

the code for a single folder i found in a tutorial online

before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (3)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added:
	print "Images Added: ", ", ".join (added)
        #main_function(added)

  if removed:
	print "Images Removed: ", ", ".join (removed)
  before = after

I need to monitor a number of folders for changes, depending on the type of file and folder it will trigger a action.
then second part of the script checks every couple of minutes for changes to the folder structure, i just need lists of all the folders monitored.

the code for a single folder i found in a tutorial online

before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (3)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added:
	print "Images Added: ", ", ".join (added)
        #main_function(added)

  if removed:
	print "Images Removed: ", ", ".join (removed)
  before = after

simplified the code to...

before = [],[]
path_to_watch.append("/path1/")
path_to_watch.append("/path2/")
path_to_watch.append("/path3/")

for item, folder in enumerate(path_to_watch):
    before[item].append("1")

Why not to do simply:

import os,time
path_to_watch='/Tony'
before = set(f for f in os.listdir(path_to_watch))
while 1:
    time.sleep (3)
    after = set(f for f in os.listdir(path_to_watch))
    added = [f for f in after if not f in before]
    removed = [f for f in before if not f in after]
    if added:
        print "Images Added: ", ", ".join (added)
    if removed:
        print "Images Removed: ", ", ".join (removed)
    before = after

after more googling i got it to work by,

import os

path_to_watch = [[0] for i in range(len(path_to_watch))]
path_to_watch.append("/path1/")
path_to_watch.append("/path2/")
path_to_watch.append("/path3/")

before = [],[]
for item, folder in enumerate(path_to_watch):
    print "i am here", item, " : ", folder
    before[item].append(dict ([(f, None) for f in os.listdir (folder)]))

miss understood how to declare the listoflist

thanks every one for your help

thanks tonyjv

not the problem but certainly a improvement on the code.

For multiple paths just wrap the list comprehension inside other one for paths and mayby register the files with os.path.realpath(f) instead of only f.

Better to put that repeated code in function as it is needed inside and out of while.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.