Python 3. File state open or closed - python

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!

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.

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.

Related

Confusion about with statement python

I have a question regarding the use of with statements in python, as given below:
with open(fname) as f:
np.save(f,MyData)
If I'm not mistaken this opens the file fname in a secure manner, such that if an exception occurs the file is closed properly. then it writes MyData to the file. But what I would do is simply:
np.save(fname,MyData)
This would result in the same, MyData gets written to fname. I'm not sure I understand correctly why the former is better. I don't understand how this one-liner could keep the file "open" after it has ran the line. Therefore I also don't see how this could create issues when my code would crash afterwards.
Maybe this is a stupid/basic question, but I always thought that a cleaner code is a nicer code, so not having the extra with-loop seems just better to me.
numpy.save() handles the opening and closing in its code, however if you supply a file descriptor, it'll leave it open because it assumes you want to do something else with the file and if it closes the file it'll break the functionality for you.
Try this:
f = open(<file>)
f.close()
f.read() # boom
See also the hasattr(file, "write") ("file" as in descriptor or "handle" from file, buffer, or other IO) check, that checks if it's an object with a write() method and judging by that Numpy only assumes it's true.
However NumPy doesn't guarantee misusing its API e.g. if you create a custom structure that's a buffer and doesn't include write(), it'll be treated as a path and thus crash in the open() call.

Approaches to loading JSON from file to dict using Python?

Using Python I'm loading JSON from a text file and converting it into a dictionary. I thought of two approaches and wanted to know which would be better.
Originally I open the text file, load the JSON, and then close text file.
import json
// Open file. Load as JSON.
data_file = open(file="fighter_data.txt", mode="r")
fighter_match_data = json.load(data_file)
data_file.close()
Could I instead do the following instead?
import json
// Open file. Load as JSON.
fighter_match_data = json.load(open(file="fighter_data.txt", mode="r"))
Would I still need to close the file? If so, how? If not, does Python close the file automatically?
Personally wouldn't do either. Best practice for opening files generally is to use with.
with open(file="fighter_data.txt", mode="r") as data_file:
fighter_match_data = json.load(data_file)
That way it automatically closes when you're out of the with statement. It's shorter than the first, and if it throws an error (say, there's an error parsing the json), it'll still close it.
Regarding your actual question, on needing to close the file in your one liner.
From what I understand about file handling and garbage collection, if you're using CPython, since the file isn't referenced anymore it "should" be closed straight away by the garbage collector. However, relying on garbage collection to do your work for you is never the nicest way of writing code. (See the answers to open read and close a file in 1 line of code for information as to why).
Your code as under is valid:
fighter_match_data = json.load(open(file="fighter_data.txt", mode="r"))
Consider this part:
open(file="fighter_data.txt", mode="r") . #1
v/s
data_file = open(file="fighter_data.txt", mode="r") . #2
In case of #2, in case you do not explicitly close the file, the file will automatically be closed when the variable ceases to exist[In better words, no reference exists to that variable] (when you move out of the function).
In case of #1, since you never create a variable, the lifespan of that implicit variable created for opening that file ceases to exist on that line itself. And python automatically closes the file after opening it.

Is it necessary to close files after writing JSON data to them in Python

If i use:
with open(Name, 'w') as outfile:
json.dump(json_object, outfile)
is it necessary to use the file.close() method afterwards?
In general cases like writing normal strings to files in python what is the consequence of not using the file.close() method?
The advantage of using the 'with' construct is precisely that you won't have to worry about closing the file here, as it will automatically be closed when the file goes out of scope.
In cpython, files close when their refcount goes to zero, even if you don't use a with statement. Depending on how you use the file object, you run the risk that circular references keep this from happening. But there are conditions like some signals where regular cleanup doesn't happen. In that case, you may loose whatever data is still in the local process file buffer that hasn't been flushed.
The least risky solution is to use with statements when possible and try/finally blocks that clean up files otherwise.

Python redirect/write output to file?

I want to write the output write the output in a file, but all I got was "None" even for words with synonyms.
Note: when I am not writing in the file the output it works perfectly fine Another note: the output appears on the screen whether i am writing to a file or not, but I get "None" in the file, is theres anyway to fix it.
[im using python V2.7 Mac version]
file=open("INPUT.txt","w") #opening a file
for xxx in Diacritics:
print xxx
synsets = wn.get_synsetids_from_word(xxx) or []
for s in synsets:
file.write(str(wn._items[s].describe()))
I tried to simplify the question and rewrite your code so that its an independent test that you should be able to run and eventually modify if that was the problem with your code.
test = "Is this a real life? Is this fantasy? Caught in a test slide..."
with open('test.txt', 'w') as f:
for word in test.split():
f.write(word) # test.txt output: Isthisareallife?Isthisfantasy?Caughtinatestslide...
A side note it almost sounds like you want to append rather than truncate, but I am not sure, so take a look at this.
The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.
file.write() is going to write whatever is returned by the describe() command. Because 'None' is being written, and because output always goes to the screen, the problem is that describe is writing to the screen directly (probably with print) and returning None.
You need to use some other method besides describe, or give the correct parameters to describe to have it return the strings instead of printing them, or file a bug report. (I am not familiar with that package, so I don't know which is the correct course of action.)

What happens if I read a file without closing it afterwards?

I used to read files like this:
f = [i.strip("\n") for i in open("filename.txt")]
which works just fine. I prefer this way because it is cleaner and shorter than traditional file reading code samples available on the web (e.g. f = open(...) , for line in f.readlines() , f.close()).
However, I wonder if there can be any drawback for reading files like this, e.g. since I don't close the file, does Python interpreter handles this itself? Is there anything I should be careful of using this approach?
This is the recommended way:
with open("filename.txt") as f:
lines = [line.strip("\n") for line in f]
The other way may not close the input file for a long time. This may not matter for your application.
The with statement takes care of closing the file for you. In CPython, just letting the file handle object be garbage-collected should close the file for you, but in other flavors of Python (Jython, IronPython, PyPy) you definitely can't count on this. Also, the with statement makes your intentions very clear, and conforms with common practice.
From the docs:
When you’re done with a file, call f.close() to close it and free up any system resources taken up by the open file.
You should always close a file after working with it. Python will not automatically do it for you. If you want a cleaner and shorter way, use a with statement:
with open("filename.txt") as myfile:
lines = [i.strip("\n") for i in myfile]
This has two advantages:
It automatically closes the file after the with block
If an exception is raised, the file is closed regardless.
It might be fine in a limited number of cases, e.g. a temporary test.
Python will only close the file handle after it finishes the execution.
Therefore this approach is a no-go for a proper application.
When we write onto a file using any of the write functions. Python holds everything to write in the file in a buffer and pushes it onto the actual file on the storage device either at the end of the python file or if it encounters a close() function.
So if the file terminates in between then the data is not stored in the file. So I would suggest two options:
use with because as soon as you get out of the block or encounter any exception it closes the file,
with open(filename , file_mode) as file_object:
do the file manipulations........
or you can use the flush() function if you want to force python to write contents of buffer onto storage without closing the file.
file_object.flush()
For Reference: https://lerner.co.il/2015/01/18/dont-use-python-close-files-answer-depends/

Categories

Resources