concatenating files using python

i have many text files with extension .txt each having three columns and i want to concatenate all one below another with ‘>>>>>’ symbol in between and want to save it in another text file Input:

file1.txt         file2.txt      file3.txt    
1.2 2.0 3.0       2.0 3.2 4.1    2.1  4.0  6.3
3.5 4.5 5.3       4.1 6.2 8.2    1.2  4.3  2.3
3.1 4.4 5.7       2.3 5.6 9.0    2.6  4.2  4.1

output:

>>>>>
1.2 2.0 3.0
3.5 4.5 5.3
3.1 4.4 5.7
>>>>>
2.0 3.2 4.1
4.1 6.2 8.2
2.3 5.6 9.0
>>>>>
2.1 4.0 6.3
1.2 4.3 2.3
2.6 4.2 4.1

i tried

import nump as np
import pandas as pd
data=np.loadtxt("file*.txt")
conc= np.concatenate(data)
np.savetxt('output',conc)

but it doesnot solve my problem.I hope experts may help me.Thanks in advance.

Answer

You don’t need pandas or numpy. They are not intended for text manipulation in general. You can try the following, which uses glob to detect files with a pattern (e.g. file*.txt).

import glob

with open('output.txt', 'w') as g:
    for filename in glob.glob('file*.txt'):
        with open(filename, 'r') as f:
            g.write('>>>>n')
            g.write(f.read())

If you happen to use shell (such as bash), then it might be more natural to use a shell script, something like:

for f in file*.txt
do
  printf '>>>>n' >> output.txt
  cat "$f" >> output.txt
done