Categories
Uncategorized

Merge all Text Files in Folder – Python

#!/usr/bin/python
import os

dirLoc="/path/to/the/folder"
files=os.listdir(dirLoc)
for f in files:
	if f.endswith(".txt"):
		data=open(f,"r")
		out=open("outPut.txt", 'a')
		for line in data:
			out.write(line)
		data.close()
		out.close()

So much easier on the terminal

$ cat /path/to/the/folder/*.txt > /path/to/the/folder/outPut.txt

One reply on “Merge all Text Files in Folder – Python”

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s