I'm reading a file, chomping the line endings. I have this code.
def strip_line(read_me=file_opened_for_reading):
return (line.strip('\n') for line in read_me)
with open('readme.txt', 'r') as file_opened_for_reading:
stripped_lines = strip_line()
[line for line in stripped_lines]
this doesn't work because the file is not opened
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in strip_line
ValueError: I/O operation on closed file
but can be fixed by passing 'file_opened_for_reading' as below
with open('readme.txt', 'r') as file_opened_for_reading:
lines = strip_line(file_opened_for_reading)
[line for line in lines]
It makes sense that the file_opened_for_reading needs to be passed to strip_line but I was expecting that the default set for 'read_me' would take care of that. Where have I gone wrong with my use of this keyword bit?