recursive file editing
Peter Otten
__peter__ at web.de
Mon Apr 5 13:15:01 EDT 2004
TaeKyon wrote:
> 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".
A minimal working example is:
import os
for path, folders, files in os.walk("/path/to/folder"):
for name in files:
filepath = os.path.join(path, name)
fileopen = file(filepath, 'r+')
fileopen.write("This works !")
fileopen.close()
You need to compose the filepath, and, yes, it's a bit clumsy.
I've written a little generator function to hide some of the clumsiness:
def files(folder):
for path, folders, files in os.walk(folder):
for name in files:
yield os.path.join(path, name)
With that the code is simplified to:
for filepath in files("/path/to/folder"):
fileopen = file(filepath, 'r+')
fileopen.write("This works !")
fileopen.close()
HTH,
Peter
More information about the Python-list
mailing list