Combine .txt Files in Python

Using the HDFS (Hadoop File System), I was able to save data from a query which the hopes of using for analysis.  From there, I moved the files using SCP to my local machine. However, I was dealing with over 700 .txt files that would be needed to be combined.  Looking at the file names,  they are is this format “000000_0” to “000770_0”.   In Unix, the simple way is to use a command such as “cat * > new_file_name”, which will combine all files.   But there could be times you don’t want all files in a directory to be combined, or need some sort of logic applied.  Using Python, here is my code to make it happen:

# create a empty file called "combined.csv" using # a text editor
output=open("combined.csv","a")
# first file:
for line in open("000000_0"):
output.write(line)

# files with 1 digit:
for num in range(1,9):
f = open("00000"+str(num)+"_0")

# f.next() # skip the header
for line in f:
output.write(line)
f.close() # not really needed

# files with 2 digits:
for num in range(10,99):
f = open("0000"+str(num)+"_0")
# f.next() # skip the header
for line in f:
output.write(line)
f.close() # not really needed

# files with 3 digits:
for num in range(100,769):
f = open("000"+str(num)+"_0")
# f.next() # skip the header
for line in f:
output.write(line)
f.close()
output.close()