Re: [Tutor] Scan Directory for files

2008-08-02 Thread Steve Poe

Fred,

What is/are the exact error message(s)?

You may want to look at the module glob.

Steve



Ar e you typing this in the python interpreter or
On Aug 1, 2008, at 10:41 PM, Fred @ Mac wrote:


Hello,

new to python, so please go easy on me!

I am using

for f in os.listdir(watch_dir):
   tree = ET.parse(f)
   for shot in tree.findall('Shot'):
..do stuff..

to scan a directory for specific files (xml files specifically).

But my script fails if, for example, a directory also exists in  
watch_dir


How can i restructure this so it only returns a list of the .xml  
files in that directory, ignores other files and or directories in  
watch_dir


Thanks!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scan Directory for files

2008-08-02 Thread Alan Gauld


Fred @ Mac [EMAIL PROTECTED] wrote 


for f in os.listdir(watch_dir):
tree = ET.parse(f)
for shot in tree.findall('Shot'):
..do stuff..

But my script fails if, for example, a directory also exists in  
watch_dir


Take a look at os.walk which allows recursive traversal of 
a directory structure.


There is a short discussion in the OS topic on my web tutor.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scan Directory for files

2008-08-02 Thread Kent Johnson
On Sat, Aug 2, 2008 at 1:41 AM, Fred @ Mac [EMAIL PROTECTED] wrote:
 Hello,

 new to python, so please go easy on me!

 I am using

 for f in os.listdir(watch_dir):
tree = ET.parse(f)

Should be ET.parse(os.path.join(watch_dir, f)) I think...

for shot in tree.findall('Shot'):
..do stuff..

 to scan a directory for specific files (xml files specifically).

 But my script fails if, for example, a directory also exists in watch_dir

 How can i restructure this so it only returns a list of the .xml files in
 that directory, ignores other files and or directories in watch_dir

The glob module can filter file names based on patterns.
os.path.isfile() will tell you if something is a file. So for example:

pattern = os.path.join(watch_dir, *.xml)
for f in glob.glob(pattern):
  if not os.path.isfile(f): #already did the join in the pattern
continue
  tree = ET.parse(f)

Jason Orendorff's path module is useful for this also though the doc
site seems to be down:
http://pypi.python.org/pypi/path.py/2.2

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Scan Directory for files

2008-08-01 Thread Fred @ Mac

Hello,

new to python, so please go easy on me!

I am using

for f in os.listdir(watch_dir):
tree = ET.parse(f)
for shot in tree.findall('Shot'):
..do stuff..

to scan a directory for specific files (xml files specifically).

But my script fails if, for example, a directory also exists in  
watch_dir


How can i restructure this so it only returns a list of the .xml files  
in that directory, ignores other files and or directories in watch_dir


Thanks!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor