combined files together
Tim Williams
tdw at tdw.net
Sat May 6 08:31:40 EDT 2006
On 06 May 2006 16:41:45 +1000, Gary Wessle <phddas at yahoo.com> wrote:
> 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?
If you want to combine text files file1 and file2 into a combined file1
>>> txt = open('file2.txt', 'r').read()
>>> open('file1.txt', 'a').write(txt)
or less easy to read
>>> open('file1.txt', 'a').write( open('file2.txt', 'r').read() )
If you want to combine file1 and file2 into a brand new new file3
>>> txt1 = open('file1.txt', 'r').read()
>>> txt2 = open('file2.txt', 'r').read()
if you have a trailing newline on file1 use
>>> txt1 = open('file1.txt', 'r').read()[:-1]
then something like
>>> open('file3.txt', 'w').write(txt1)
>>> open('file3.txt', 'a').write(txt2)
or maybe
>>> open('file3.txt', 'w').write(txt1+txt2)
If you already have file3 and want to keep its contents, change the
writes above ( 'w' ) to appends ( 'a' )
HTH :)
More information about the Python-list
mailing list