Skip to content

Fix path_to_file_list to read lines from file#400

Open
jungjun0708 wants to merge 1 commit intoagwaBom:mainfrom
jungjun0708:make_corrections_in_main_py
Open

Fix path_to_file_list to read lines from file#400
jungjun0708 wants to merge 1 commit intoagwaBom:mainfrom
jungjun0708:make_corrections_in_main_py

Conversation

@jungjun0708
Copy link
Copy Markdown

In main.py, I modified the path_to_file_list function.

Original code (lines 5–6):
li = open(path, 'w')
return lines

New code (lines 5–9):
lines: List[str] = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
lines.append(line.rstrip("\n"))
return lines

Why the original code was wrong:

  1. The file was opened in write mode ('w'), which truncates the file and does not read any contents.
  2. The function never used the variable li and tried to return an undefined variable lines, which would raise a NameError.
  3. Because of these issues, the function did not satisfy its own docstring (“Reads a file and returns a list of lines in the file”).

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