combined files together

Gary Herron gherron at islandtraining.com
Sat May 6 03:00:24 EDT 2006


Gary Wessle wrote:

>Hi
>
>is there a module to do things like concatenate all files in a given
>directory into a big file, where all the files have the same data
>formate?
>name address phone_no.
>
>or do I have to open each, read from old/write-or-append to new ...
>
>thanks
>
>  
>
There's hardly enough code here to make a module out of this:

combined = open('...', 'wb')
for name in os.listdir(path):
infile = open(os.path.join(path,name), 'rb')
for line in infile:
combined.write(line)

It could be more efficient by reading larger chunks than single lines, 
and could be more accurate by closing both input and output files when 
done, but you get the point I hope.

On the other hand, if you've got the right OS, you might try something like:
os.system("cat * > combined")

Gary Herron






More information about the Python-list mailing list