Use fileinput. Say you have a python file merge.py
with the following code, you could call it like so merge.py dir/*.txt
. File merged.txt
gets written to current dir. By default, fileinput
iterates over the list of files passed on the command line, so you can let the shell handle globbing
#!/usr/bin/env python
import fileinput
with open('merged.txt', 'w') as f:
for line in fileinput.input():
f.write(line)