recursive file editing
TaeKyon
mikalzetTogli at interfree.it
Mon Apr 5 12:40:25 EDT 2004
Il Sat, 03 Apr 2004 17:22:04 -0800, Josiah Carlson ha scritto:
> for thing in os.walk('mydir'):
> filehandle = file(thing, 'r+')
I'm such a newbe I can't get it to work. Here is an example:
in empty directory foo I touch a b c d;
suppose I want to write "This works !" in each of these files.
I run python
>>> import os
>>> for thing in os.walk('foo'):
... thingopen = file(thing,'r+')
... thingopen.write("This works !")
... thingopen.close()
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: coercing to Unicode: need string or buffer, tuple found
And in fact:
>>> for thing in os.walk('foo'):
... print thing
...
('foo', [], ['a', 'b', 'c', 'd'])
which is a tuple, I suppose.
Selecting thing[2] doesn't help, because it now complains of it being a
list.
In the end I get this to work:
for filetuple in os.walk('foo'):
... for filename in filetuple[2]:
... fileopen = file(filename, 'r+')
fileopen.write("This works !")
fileopen.close()
which seems a bit of a clumsy way to do it.
And besides it only works if I run python from directory foo,
otherwise it tells me "no such file or directory".
--
Michele Alzetta
More information about the Python-list
mailing list