Using more_itertools.with_iter
, it is possible to open, read, close and assign an equivalent output
in one line (excluding the import statement):
import more_itertools as mit
output = "".join(line for line in mit.with_iter(open("pagehead.section.htm", "r")))
Although possible, I would look for another approach other than assigning the contents of a file to a variable, i.e. lazy iteration - this can be done using a traditional with
block or in the example above by removing join()
and iterating output
.