[Tutor] walking directories

Ryan Davis ryan at acceleration.net
Fri Jan 7 23:58:28 CET 2005


I think you want to be doing something like:

>>>for r,d,f in os.walk('.'):
...	for filename in f:
...		print os.path.join(r,filename)

I think that would give you the full path of every file, and then you can open it, do a regex substitution or whatever close it and
keep going.

Thanks,
Ryan 

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf Of Nick Lunt
Sent: Friday, January 07, 2005 5:32 PM
To: python tutor
Subject: [Tutor] walking directories

Hi folks,

Im running python2.4 on linux.

I have many python scripts in various directories of varying depth under my home directory, and I need to change one line in each of
these files.

I thought about using os.walk with os.path.join eg

>>> for r,d,f in os.walk('.'):
...     print os.path.join(r,d,f)

but I get this error
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
  File "/usr/local/lib/python2.4/posixpath.py", line 60, in join
    if b.startswith('/'):
AttributeError: 'list' object has no attribute 'startswith'

which makes sense.

I know I can use glob.glob on the current directory, but Im struggling to see how I can access these files, change them and save the
changes.

Im also a little annoyed with myself cos I feel that I really should know how to do this by now.

If anyone can just give me a pointer in the right direction I would be very grateful.

Many thanks
Nick .

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list