-2

I have multiple files in path /opt/data/file1.txt file2.txt file3.txt

I want to append all context in the single file like path /opt/alltxt.txt file content like

path /opt/data/file1.txt
"file1 context"    

path /opt/data/file2.txt
"file2 context"

path /opt/data/file2.txt
"file2 context"
Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
Aftab Ali
  • 27
  • 1
  • 6

1 Answers1

0

Try the below code. It may help you

import os
import glob
def remove_newlines(fname):
    flist = open(fname).readlines()
    return [s.rstrip('\n') for s in flist]

my_file=open("all_content_file.txt", "w")
for i in glob.glob('./*.txt'):
    my_file.write("path")
    my_file.write(os.path.abspath(i))
    my_file.write(" ")
    for j in remove_newlines(os.path.abspath(i)):
        my_file.write(j)
        my_file.write(" ")
    my_file.write("\n")