1

Started to learn Python! Finally!

The line in question:

readMyFile=open("myFile.txt","r").read()

The line above reads the txt file into variable readMyFile. What I need to know:

How can i get the state of the myFile.txt file (if it's open or closed)? And how to close it if it's open? I know how to close it in case when I have:

readMyFile=open("myFile.txt","r")
readMyFile.close

But in the case above, variable contains text from the file, not the file object.

Thanks in advance!

2 Answers 2

3

When you write this:

readMyFile=open("myFile.txt","r").read()

… you can't get the state of the file, or close it, or anything else. You didn't store it, so you have no way to access it. There is no magic that lets you get back information that you threw away (otherwise, there'd be no point in throwing away information, and things like garbage collection couldn't possibly work).

The solution is to just not do that: any value that you want to use, store it in a variable (or a list or dict member, or an attribute of an instance, or whatever—just store it somewhere) and then (as you already know from your question) you can use it:

>>> myfile = open("myFile.txt","r")
>>> readMyFile = myFile.read()
>>> myFile.closed
False
>>> myFile.close()
>>> myFile.closed
True

Although in this case, there's an even better answer:

>>> with open("myFile.txt", "r") as myFile:
...     readMyFile = myFile.read()

Now, myFile automatically gets closed at the end of the with statement, so you never have to worry about it.

Or, even better, you can wrap this in a function:

>>> def readfile(name):
...     with open(name) as myFile:
...         return myFile.read()
>>> readMyFile = readfile("myFile.txt")

… and none of your code even has to think about file objects, except the two lines of code inside readfile.


In a comment, you asked:

readMyFile=open("myFile.txt","r").read() is equal to 'with' solution in regard, that in both cases file will be closed and throwed away?

No, they're not equivalent.

  • The first version throws away your reference to the file object, so the file is now garbage—but you're leaving it up to Python to figure out that it's garbage and clean it up (which closes the file) for you whenever it notices and gets around to it.

  • The with version tells Python to clean up as soon as the with statement finishes.

As it happens, in this particular example, with CPython (the Python implementation you're probably using if you don't know which one you're using), Python will figure out that the value is garbage, and close the file, at the end of the statement, so there's no difference.

But that's not true in slightly different cases. And you can't actually know which cases it's true in without knowing a bit about the internals of how CPython works. And even then, if you run the same code in, say, PyPy instead of CPython, it will do something different. And that's why many Python programs used to mysteriously sometimes fail to write the last 300 bytes of a text file, or Python web servers would occasionally fail by running out of file handles, and so on—which is exactly the problem the with statement solves: you tell Python how long you need the value to live just by a simple indent, and it makes sure that value is cleaned up at the end.

4
  • Read about 'with'. So basically, readMyFile=open("myFile.txt","r").read() is equal to 'with' solution in regard, that in both cases file will be closed and throwed away? - that's what I wanted to know
    – ValeriyB
    Commented Jul 5, 2018 at 7:29
  • @ValeriyB No, they're not equivalent. Let me edit the answer to explain.
    – abarnert
    Commented Jul 5, 2018 at 7:32
  • So it's just a better practice to not to do it. And we can't learn if it's opened in this case, but it closed. Thanks!
    – ValeriyB
    Commented Jul 5, 2018 at 7:37
  • @ValeriyB The best practice is to use a wrapper function, or a with statement, or both. When neither of those will work, then the best practice is to keep track of the file object manually, by storing it in a variable. But usually one of those will work.
    – abarnert
    Commented Jul 5, 2018 at 7:44
1

try:

with open("myFile.txt","r") as fd:
     readMyFile = fd.read()

Here you don't need to worry about closing the file. Sometimes too much files opened and not closing can cause some issues. Using a with statement Python will automatically close your file after with block.

Not the answer you're looking for? Browse other questions tagged or ask your own question.