1

Newb to python, a small doubt. Have multiple text files(100s) each with multiple lines. I want to combine all the files into a single txt file. Presently, I am using this code: import fileinput import glob

file_list = glob.glob("*.txt")

with open('result.txt', 'w') as file:
    input_lines = fileinput.input(file_list)
    file.writelines(input_lines)

But, all the contents in the result file are combined without any newlines. I am unable to add newlines '/n' when I write the file. Converting input_lines to str and concatenating with \n doesn't work. Thanks for the help. Using the previous question's code doesn't put all the lines in the specified order. Like, 1.txt contains q w e r t, 2.txt contains a s d f g, 3.txt contains g h j k l and so on. The output file must be like, q w e r t(\n) a s d f g(\n) g h j k l and not like q w e r ta s d f gg h j k l which the previous codes do. Newlines should be inserted after every file reading. Thanks!

theseeker1
  • 13
  • 3

0 Answers0