There are several ways, for example, you can just use f = open(...) and del f .
If your version of Python supports it, you can also use the with statement:
with open("file.txt", "r") as f: line = f.readlines()[7]
This automatically closes your file.
EDIT: I do not understand why I am understated to explain how to create the correct way to work with files. Perhaps "without assigning a variable" is simply not preferable.
source share