[Tutor] walking directories

Nick Lunt nick at javacat.f2s.com
Fri Jan 7 23:32:14 CET 2005


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 .



More information about the Tutor mailing list