Under which circumstances will the python f.readlines method fail? - python

I use the code below to read in a text file (always a few thousand lines long). Is the except Exception as e block unnecessary?
try:
in_file=open(in_file,'rU')
try:
in_content=in_file.readlines()
except Exception as e:
sys.stderr.write('Error: %s\n' % e.message)
sys.exit(1)
finally:
in_file.close()
except IOError:
sys.stderr.write('I/O Error: Input file not found.')
sys.exit(1)
Also please tell me of the circumstances under which the file.readlines() method in Python will fail?

I believe that IOError is the only possible thing that can happen. This covers both the file not existing and inadequate permissions. Any python reference I have seen only has IOError with files :). I'm not sure by what you mean with the stack trace, since it seems to just print the error itself?
import sys
try:
with open("in_file",'rU') as in_file:
in_content=in_file.readlines()
except Exception as e: #Should be replaceable with IOError, doesn't hurt to not
sys.stderr.write('%s\n' % e)
sys.exit(1)

The pythonic way to read file looks like this:
with open(in_file_name,'rU') as in_file:
in_content = in_file.readlines()
This should give you all the benefits of your code. So you don't need to worry about what kind of errors can occur. Python will take care of it. A file opened using the with statement will be closed in case of an exception.

Related

Use try and except to skip a file

I am defining a list of nc files between 2 dates:
inlist = ['20180101.nc’, ‘20180102.nc’, ‘20180103.nc’]
Let’s suppose that the file in the middle (‘20180102.nc’) does not exist.
I am trying to use an exception and skip it and continue with the rest, but I can’t manage.
Here’s my code. Note that ncread(i)[0] is a function that reads one variable, which is then concatenated in xap:
xap = np.empty([0])
try:
for i in inlist:
xap=np.concatenate((xap,ncread(i)[0]))
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
continue
This code always stops when it tries to read the file that does not exist (‘20180102.nc’).
How can I skip this file and continue concatenating only the files that exist?
Thanks in advance.
Your try/except is placed on the wrong level, you want to try the read, and when it fails continue the loop. This means the try/except must be inside the loop:
xap = np.empty([0])
for i in inlist:
try:
xap=np.concatenate((xap,ncread(i)[0]))
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
continue
if you also consider another way, here is a simple way to reach your purpose.
use this to operate the system
import os
list all your file in current directory(you should change to your object path)
filelist=os.listdir("./")
inlist = ['20180101.nc', '20180102.nc', '20180103.nc']
xap = np.empty([0])
for i in inlist:
##** only read the "i" in filelist**
if i in filelist: xap=np.concatenate((xap,ncread(i)[0]))
You need to change IOError to FileNotFoundError:
xap = np.empty([0])
try:
for i in inlist:
xap=np.concatenate((xap,ncread(i)[0]))
except FileNotFoundError as e:
print "FileNotFoundError({0}): {1}".format(e.errno, e.strerror)
continue

fake `.gz` raise IOError, 'Not a gzipped file'

Let's say I have a fake gz file test.bson.gz generated by echo "hello world" > test.bson.gz, and I have tried:
try:
bson_file = gzip.open('test.bson.gz', mode='rb')
except:
print("cannot open")
No exception will be caught here. (Really strange, since this is not a valid gz...)
Then I do:
data = bson_file.read(4)
I will get:
File "/usr/lib/python2.7/gzip.py", line 190, in _read_gzip_header
raise IOError, 'Not a gzipped file'
IOError: Not a gzipped file
Is there any way that I can determine (even catch error) whether this .gz is valid when I try to open it, not wait until I wanna read it?
Thanks!
You can use gzip.peek(n):
Read n uncompressed bytes without advancing the file position.
try:
bson_file = gzip.open('test.bson.gz', mode='rb')
bson_file.peek(1)
except OSError:
print("cannot open")
That way you will catch the error without consuming the file contents.
Hint: You should avoid catching all errors unconditionally. I added except OSError, because IOError was merged to OSError in Python 3.3 - see PEP3151.

How can I close a file handle while function exit with exception in Python?

For instance, I have a function like:
def example():
fp = open('example.txt','w+')
fp.write(str(1/0))
fp.close()
Then it will throw an exception because 1/0 is not defined. However, I can neither remove example.txt nor modify example.txt. But I have some important data in Python, so that I can't simply kill Python and run it again.
How could I finish opening the file when the function finish with an exception.
What shall we do if we didn't place a try:.. except:.. ?
with open('example.txt','w+') as fp:
try:
fp.write(...)
except ZeroDivisionError as e:
print('there was an error: {}'.format(e))
Using the with context manager any files opened by it will be closed automatically once they go out of scope.
You can wrap that in a try/except to handle the error and close the file reader before the program ends.
def example():
fp = open('example.txt', 'w+')
try:
fp.write(str(1/0))
except ZeroDivisionError:
fp.close()
fp.close()
Edit: The answer by #IanAuld is better than mine. It would be best to accept that one.

Detecting for invalid file inputs, Python

I have an assignment to write a Python script which "detects whether the file is readable or not".
I am stuck as to which exceptions I should run. Let's say the input file is intended to be a text file, with extension *.txt
What is the exception I should raise? I suspect there should be multiple. At the moment, I have:
with open('example_file.txt") as textfile:
if not textfile.lower().endswith('.txt'):
raise argparse.ArgumentTypeError(
'Not a text file! Argument filename must be of type *.txt')
return textfile
However, that only checks the file extension. What else could I possibly check? What is the standard for file I/O in Python?
To check whether the file exists:
import os.path
if os.path.exists('example_file.txt'):
print('it exists!')
Beyond this, successfully opening the file will demonstrate readability. The built-in open raises an IOError exception if it fails. Failure can occur for more than one reason, so we must check whether it failed due to readability:
import errno
try:
textfile = open('example_file.txt', 'r')
textfile.close()
print("file is readable")
except IOError as e:
if e.errno == errno.EACCES:
print("file exists, but isn't readable")
elif e.errno == errno.ENOENT:
print("files isn't readable because it isn't there")
The relevant section of the docs on file permissions. Note that the use of os.access to check readability before calling open is discouraged.

try...except...else v nested try...except

Why is else clause needed for try statement in python ?
Taking it forward:
try:
f = open('foo', 'r')
except IOError as e:
error_log.write('Unable to open foo : %s\n' % e)
else:
data = f.read()
f.close()
It occurs to me that the corner case solved by else clause still can be avoided by a nested try...except avoiding the need of else? :
try:
f = open('foo', 'r')
try:
data = f.read()
f.close()
except:
pass
except IOError as e:
error_log.write('Unable to open foo : %s\n' % e)
try..except..else may not be needed, but it can be nice. In this case, the try..except..else form is distinctly nicer, in my opinion.
Just because you can do without an element of syntax, doesn't make it useless. Decorator syntax is purely syntax sugar (the most obvious example, I think), for loops are just glorified while loops, et cetera. There's a good place for try..except..else and I would say this is one such place.
Besides, those two blocks of code are far from equivalent. If f.read() raises an exception (disk read error, data corruption inside the file or some other such problem), the first will raise an exception as it should but the second will lose it and think that everything has worked. For myself, I would prefer something more along these lines, which is shorter and still easier to understand:
try:
with open('foo', 'r') as f:
data = f.read()
except IOError as e:
error_log.write('Unable to open foo : %s\n' % e)
(This assumes that you want to catch errors in file.read and file.close. And I can't really see why you wouldn't.)
Actually, is not always needed you can simply do:
f = None
try:
f = open('foo', 'r')
except IOError:
error_log.write('Unable to open foo\n')
if f:
data = f.read()
f.close()

Categories