Why can't handle "io.UnsupportedOperation" error by try-except - python

Running the following code will raise an io.UnsupportedOperation error, because the file is open in "write" mode -
with open("hi.txt", "w") as f:
print(f.read())
The output is -
io.UnsupportedOperation: not readable
So, we can try to cover this up by doing this -
try:
with open("hi.txt", "w") as f:
print(f.read())
except io.UnsupportedOperation:
print("Please give the file read permission")
Output -
NameError: name 'io' is not defined
Even removing "io." spits out the same error -
try:
with open("hi.txt", "w") as f:
print(f.read())
except UnsupportedOperation:
print("Please give the file read permission")
Output -
NameError: name 'UnsupportedOperation' is not defined
Why isn't it working? Isn't "io.UnsupportedOperation" an error?

The io.UnsupportedError is found in the module io. Therefore, before we can use it, we need to import io
import io
then when we are testing for the error in the try except clause we can use io.UnsupportedError.
This gives us:
import io
try:
with open("hi.txt", "w") as f:
print(f.read())
except io.UnsupportedOperation as e:
print(e)
or if you are only using the io module for checking this specific error.
from io import UnsupportedError
try:
with open("hi.txt", "w") as f:
print(f.read())
except UnsupportedOperation as e:
print(e)

Related

Python - test if a file has already been open

I would testing if a file has already been open before writing.
Here my code :
with open(file_five, 'w') as f:
f.write(xml)
I would something as this code example :
if "file_five has already been open"
with open(file_five, 'w') as f:
f.write(xml)
else:
...
There are two ways:
1-> For Excel specific
try:
myfile = open("file_five.csv", "r+") # or "a+", whatever you need
except IOError:
print "Could not open file! !"
with myfile:
do_stuff()
2 -> For any file (Rename approach)
import os
try:
os.rename('file.xls', 'tempfile.xls')
os.rename('tempfile.xls', 'file.xls')
except OSError:
print('File is still open.')

How to avoid partial file if exception thrown during json dump?

The following code throws
TypeError: Object of type 'datetime' is not JSON serializable
which I know how to resolve. However my real question is how to cleanly structure the code to avoid a partial file if any exception occurs in json.dump.
import datetime
import json
def save(data):
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
data = dict(sometime=datetime.datetime.now())
save(data)
The above code throws an exception and results in a partial file like:
{"sometime":
Should I dumps to a string first in a try/except? If so are there any memory implications to be aware of? Or delete the file in an except block?
Use a try/except block like:
Code:
def save_json(data, filename):
try:
with open(filename, 'w') as outfile:
json.dump(data, outfile)
except:
try:
os.unlink(filename)
except FileNotFoundError:
pass
and if you want to preserve the exception:
def save_json(data, filename):
try:
with open(filename, 'w') as outfile:
json.dump(data, outfile)
except:
if os.path.exists(filename):
os.unlink(filename)
raise
Test Code:
import datetime
import json
import os
data = dict(sometime=datetime.datetime.now())
save_json(data, 'data.txt')
That depends on whether your JSON data is under your control or from unknown source. If it’s from somewhere you can’t predict, use try...except... block. Otherwise, fix your program to make it always available to serialize.

Why doesn't "try, except" work with classic "open(fname, 'r')" in python?

I have a function that opens a file and returns an opened file object.
def read_any():
try:
opened = gzip.open(fname, 'r')
except IOError:
opened = open(fname, 'r')
return opened
When I attempt to run this function on some non-zipped file except condition does not get triggered and the function crashes with the message: IOError: Not a gzipped file.
Ok, now I try and do the same with with statement:
def read_any2():
try:
with gzip.open(fname, 'r') as f:
return f.read()
except IOError:
with open(fname, 'r') as f:
return f.read()
Now, if I try to run the same file the function works as intended.
Can you explain why doesn't except condition get triggered?
To see what's going on, test it in a REPL:
>>> import gzip
>>> f = gzip.open('some_nongzipped_file', 'r')
You will see that this doesn't raise an error. Once you, however, read from the object:
>>> f.read()
... (snip)
OSError: Not a gzipped file
, it raises the error.
In short: Simply creating the file object doesn't read anything from the file yet, and thus doesn't know if it should fail or not.
Since in the first example you just return the file object, when you try to read from it later it will raise the exception there (outside your raise-except block). In your second example you return f.read() which reads and therefore raises the exception. It has nothing to do with the with block, as you can see if you remove it:
def read_any_mod():
try:
opened = gzip.open(fname, 'r')
return opened.read()
except IOError:
opened = open(fname, 'r')
return opened.read()

Python: Readline returns error after 10 lines

I'm trying to use readline on file in a for loop. The problem is that I start getting I/O errors. It seems that I get I/O error after 10 readlines.
Here is my function:
def getAll():
with open("nodes2.txt", "r+") as f:
for i in range(0, 200):
print "**%s**"%(i)
try:
file = f.readline()
file = file[:-1]
# print "*%s*" % (file)
entities = getAllPagesEntities(file)
# print entities
for en in entities:
try:
dict = getFirmAttributes(en)
printToFile(dict)
except Exception,e:
with open("log_getFirmAttributes.txt","a") as f:
f.write(str(e))
f.write("\n")
except Exception,e:
with open("log_readFile.txt","a") as f:
f.write(str(e))
f.write("\n")
Here is a printed catched exception:
I/O operation on closed file
I think that this problem can't be caused by another used functions so I don't attach them here. I thought that it is caused by the file used but when I try to readline 200 and print them, everything works perfect.
with open("nodes2.txt", "r+") as f:
for i in range(0, 200):
print f.readline()
Have you any idea what could be the problem? Thanks
Following lines in the except block overwrites f causing open file to be closed.
with open("log_readFile.txt","a") as f:
f.write(str(e))
f.write("\n")
Change the name f for the file for appending to another name will solve the problem:
with open("log_readFile.txt", "a") as logf:
logf.write(str(e))
logf.write("\n")

Python file copy exception catching

I have a txt file called test.txt with 4 lines in it. I want to copy lines containing the word 'exception' into a new file from command line argument. I have managed this far. But I also want to exception handle this for IO error. That is if from the command line, somebody misspells the word test.txt, it will throw an exception. This is my current code. Please help! I'm a beginner. Presently, if I misspell it intentionally, it is not showing the error message I intend it to show.
import sys
def Cat(filename):
try:
f = open(filename, 'rU')
for line in f:
print (line),
return 3
except IOError:
print('\nIO error!!', filename)
def main():
f1 = open(sys.argv[1])
f2 = open(sys.argv[2], 'w')
for line in f1:
if 'exception' in line:
f2.write(line)
if __name__ == '__main__':
main()
First check if source file exists and readable -
if not (os.path.exists(file1) and os.access(file1, os.R_OK)):
print "file1 does not exists or not readable"
sys.exit(1)
else:
//good to go
and then handle writing of destination file with try except block.
You need to put the open() inside a try-except block, just as you did in Cat().
Currently, you are not calling Cat(), and the open() in main() is not inside a try-except block.
Since you said you are a beginner in Python I'll assume this a sort of "learning code". So, I won't tell you anything about the design. Also, you should do what #NPE says too.
You can try this in your main function in order to reach your goal:
def main():
filename = open(sys.argv[1])
if filename != "test.txt":
raise Exception("Here goes the message error you want to show")
f2 = open(sys.argv[2], 'w')
for line in f1:
if 'exception' in line:
f2.write(line)
You forgot to call Cat()
before
f2 = open(sys.argv[2], 'w')
Cat(f1)
for line in f1:
and in the Cat function you will need to raise exception to stop the execution
print('\nIO error!!', filename)
raise IOError('Invalid filename')

Categories

Resources