wra wra -4 years ago 106
Python Question

How to use glob.glob to combine txt files and add a string to break up the txt?

I'd like to combine a bunch of txt files but I would also like to add something at the end of each file so I know where one file ends and the other begins (in this case I used "###############")

If I use individual files and read through them, this code works, but I would just like to be able to read through a directory of files by using glob.glob. When I do so, I'm getting a blank outfile. Can someone please explain why this is happening?

Thanks!

import glob
with open("concatenated.txt",'w') as outfile:
for files in glob.glob('\\home\\wra\\Desktop\\combine\\*.txt'):
for fname in files:
with open(fname) as infile:
outfile.write(infile.read() +"\n\n##################\n\n")

Answer Source

2 problems (and 1 warning):

  • Unix paths should have '/' as separator, not \\. Unix path format might work on Windows, but not the other way around.
  • You can iterate directly on glob.glob. You could use either filenames = glob.glob(...) or for filename in glob.glob(...)
  • infile.read() can be dangerous if infile is huge. You could write the file line by line.

import glob
with open("concatenated.txt",'w') as outfile:
    for filename in glob.glob('/home/wra/Desktop/combine/*.txt'):
        with open(filename) as infile:
            for line in infile:
                outfile.write(line)
        outfile.write("\n\n##################\n\n")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download