Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the recommended way to parse files in Python is
not
I'd recommend changing the code to something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how file works in python the
fileread
variable will not be considered as false when end of file is reached. This meanswhile fileread:
will be an infinite loop. Moreover here we want to remove only the empty lines at the start of the file which is exactly what it was doing. Code was ok just didn't think of empty file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this should probably be rewritten to use for loops instead of while loops. It looks like
for line in file
is the recommended way to iterate through a file line by line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact I agree that for loop are more idiomatic in file reading in pythonbut both can be found, moreover here changing the while loop wouldn't be recommended because we are first skipping lines until we find a non empty line then skipping until we find a non header line. Those until are for me a sign that while loops are more appropriate. Moreover I don't think a refactor of the file would be a good idea when the fix is simply modifying one line and therefore has no link with the refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I tend to get scared of
while
loops that don't have some guaranteed termination condition, but it's hard to argue with the simplicity of a one line fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
line != ""
is in fact the guaranteed termination because we are sure it will evaluate to false at some point because the file as a finite number of lines and each loop will consume one line hence it will either terminate the loop because of the other condition or reach the end of the file and terminate the loop because of this condition.