Combined output is not displaying all the files it duplicated

Question:
The combined output file is not showing all the messages from my other files
Repl link:
https://replit.com/@jonathanessombe/learn-1#combined_output.txt

#main
import os

with open('combined_output.txt', 'w') as fo:
  files = sorted(os.listdir('journal'))
  for input_file in files:
    with open(F'journal/{input_file}', 'r') as fi:
      fo.write(fi.read())
      fo.write('\n')
      fo.write(F'----------------------journal/{input_file}----------------------')
      fo.write('\n')


#day1.txt
Today I learned how to use the open function in python

#day2.txt
I wrote some Python today

#day2_afternoon.txt
I wrote even more python

#day3.txt
Today I planned to write some code, but I watched Youtube instead.

Your day3.txt in the journal directory appears to be empty. This may be the reason you’re not getting that output.

Hey @jonathanessombe!

Try this and see if it works:

import os

with open('combined_output.txt', 'w') as fo:
    files = sorted(os.listdir('journal'))
    for input_file in files:
        with open(f'journal/{input_file}', 'r') as fi:
            content = fi.read()
            fo.write(content)
            fo.write('\n')
            fo.write(f'----------------------journal/{input_file}----------------------')
            fo.write('\n')

It reads each of the files then writes it to the combined output. I hope this helps!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.