[Tutor] monitor number of files in a folder
Νικόλαος Ράπτης
airscorp at otenet.gr
Thu Aug 6 23:25:50 CEST 2009
Well, os.listdir doesn't include "." or ".." anyway as you can see here
<http://docs.python.org/library/os.html#os.listdir>
So, just a simple len(os.listdir(my_directory)) will suffice..
A couple more points:
-It's better looking and more readable (to me that means Pythonic) to
store that ugly path to a variable first.
-Whenever you see yourself repeating a chunk of code, make a function
out of it!
So, your program now is
------------------------------
import os, time
my_directory =
'/Volumes/sgtb/lac/comps/Z353_002/renders/Z353_002_comp/Z353_002_comp_v04/2048x1556'
while len(os.listdir(my_directory)) <= 8:
print 'waiting'
time.sleep(5)
print 'still waiting'
time.sleep(5)
print 'eight'
-------------------------------
Nick (also, this is my first post)
pedro wrote:
> I got it working but it seems little clunky. If I have a folder with 6
> files in it, python will print out "waiting, still waiting, waiting,
> still waiting..." if I add 3 new file to the folder python prints
> "eight". This is the basic behaviour I want but is there a more
> pythonic way to write this.
>
>
>
> import os, time
>
> theFilesAsList = []
>
> for anItem in
> os.listdir('/Volumes/sgtb/lac/comps/Z353_002/renders/Z353_002_comp/Z353_002_comp_v04/2048x1556'):
>
>
>
> if anItem[0] != ".":
> theFilesAsList.append(anItem)
> theNumberOfImages = len(theFilesAsList)
>
> while theNumberOfImages <= 8:
> print 'waiting'
> time.sleep(5)
> print 'still waiting'
> time.sleep(5)
> theFilesAsList = []
> for anItem in
> os.listdir('/Volumes/sgtb/lac/comps/Z353_002/renders/Z353_002_comp/Z353_002_comp_v04/2048x1556'):
>
>
>
> if anItem[0] != ".":
> theFilesAsList.append(anItem)
> theNumberOfImages = len(theFilesAsList)
> print 'eight'
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list