Skip to content

Fix file reading and writing functions in main.py#392

Open
Kimhyojung0810 wants to merge 2 commits intoagwaBom:mainfrom
Kimhyojung0810:main
Open

Fix file reading and writing functions in main.py#392
Kimhyojung0810 wants to merge 2 commits intoagwaBom:mainfrom
Kimhyojung0810:main

Conversation

@Kimhyojung0810
Copy link
Copy Markdown

This PR fixes two incorrect functions in main.py so that the script can properly read english.txt and german.txt and generate the required concated.json output for the assignment.

  1. path_to_file_list
  • What line of code I changed

Original code

li = open(path, 'w')
return lines

Updated code

with open(path, 'r', encoding='utf-8') as f:
    lines = [line.rstrip('\n') for line in f]
return lines
  • Why the original code was wrong
    The file was opened in write mode 'w', which clears the file and prevents reading. No lines were actually read from the file. The variable lines was never defined, causing a NameError. As a result, the function could not return the English/German lines needed to build the JSON output.
  1. write_file_list
  • What line of code I changed

Original code

with open(path, 'r') as f:
    for file in file_list:
        f.write('\n')

Updated code

with open(path, 'w', encoding='utf-8') as f:
    for line in file_list:
        f.write(line + '\n')
  • Why the original code was wrong
    The file was opened in read mode 'r', so nothing could be written to the output file. The loop wrote only newline characters and did not write the actual strings in file_list. Because of this, the expected JSON lines would never appear in concated.json.

With these two fixes, the script now correctly reads both input files and writes the expected JSON lines into concated.json, completing the assignment requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant