File has zero lines in else block of python try except code - python

I have a simple python code to open the .csv file and check for exceptions.
The file exists in my current folder and it has more than 2 lines of data.
But the for loop in the else part is not executing.. because I'm getting zero lines to count.
# Base class for defining your own user-defined exceptions.
class Error(Exception):
'''Base class for other exceptions'''
pass
# own exception class as a subclass of error
class EmptyFileError(Error):
pass
# raise error
try:
# open the file (no error check for this example).
thefile = open('people.csv')
# count the number of lines in file.
file_content = thefile.readlines()
line_count = len(file_content)
# If there is fewer than 2 lines, raise exception.
if line_count < 2:
raise EmptyFileError
# Handles missing file error.
except FileNotFoundError:
print('\n There is no people.csv file here')
# Handles my custom error for too few rows.
except EmptyFileError:
print('\nYour people.csv does not have enough stuff')
# Handles all other Exceptions
except Exceptions as e:
# Show the error
print('\n\nFailed: The error was '+str(e))
# Close the file
thefile.close()
else:
print(thefile.name)
# file must be open if we got here
for one_line in file_content:
print(list(one_line.split(',')))
thefile.close()
print('Success')
I was able to see the output of the file name and success message from the else part but not the for loop part. There were no exceptions occurred and so the file was never closed before else part.
What could be the problem?
Solved with the help of #Ralf answer.

You already consumed all the lines of the file by calling thefile.readlines(); when you start the loop for one_line in thefile: there are no more lines to read, so the loop never gets executed.
Possible solution: use a variable to hold the file contents.
line_list = thefile.readlines()
line_count = len(line_list)
and the iterate over that:
for one_line in line_list:
Here are some related questions with more info:
Read multiple times lines of the same file Python
Why can't I call read() twice on an open file?

Related

UnboundLocalError when I try to open an invalid file name

I have written a program that takes a file name and returns numbers and it works as expected when the file name is inputted correctly but when I try to purposely write an incorrect file name, instead of giving me a "error: file not found" from the exception, it will say:
def getFile(fileName):
lines = []
try:
infile = open(fileName, 'r')
if infile != None:
for line in infile:
lines.append(line)
except IOError:
print('Error: file not found.')
finally:
infile.close()
return lines
If an exception is raised by open, the local variable infile is never declared let alone assigned, so the attempt to call infile.close() in the finally block will raise an UnboundLocalError as you see here. You can 'fix' this somewhat by declaring infile with some special uninitialized value (e.g. None) and checking explicitly like so:
def getFile(fileName):
lines = []
infile = None
try:
infile = open(fileName, 'r')
for line in infile:
lines.append(line)
except IOError:
print('Error: file not found.')
finally:
if infile is not None:
infile.close()
return lines
Alternatively, since file objects are context managers, you can write something like:
def getFile(fileName):
lines = []
try:
with open(fileName, 'r') as infile:
for line in infile:
lines.append(line)
except IOError:
print('Error: file not found.')
return lines
... which will ensure infile is closed in a more syntactically concise & structured manner.
Note that on failure open raises an OSError (e.g. a FileNotFoundError) rather than returning None, so your existing check is redundant.
Additionally, an IOError might be raised when iterating over the file rather than opening it initially, so the error message printed may be incorrect in those circumstances.
Lastly, since infile is an iterable, you can iteratively construct a list from it readily using the constructor that accepts an iterable directly like so:
return list(infile)

How to fix error exception to allow for retries without the exception looping in Python

I'm attempting to write error handling in Python 2.7 for when an IOError exception is raised after a user enters a filename.
I have tried a couple of solutions our there on the internet including:
How to retry after exception?
Get a Try statement to loop around until correct value obtained
This is my original code:
while True:
try:
with open (userFile, 'r') as txtFile:
for curLine in txtFile:
curLine = curLine.rstrip("\n\r")
idList.append(curLine)
except IOError:
print("File does not exist")
Whenever the IOError exception is raised it goes into an infinite loop, printing "File does not exist" over and over again. In the instance where I limit the attempts by adding a range, it goes through that range, printing over and over again and then exits the script. Does anyone have an idea why this keeps looping when the exception is raised?
This will be much easier if you split the separate concerns into functions, i.e. (i) warning the user if a file doesn't exist and (ii) reading the contents of the file into a list of lines:
def read_file(f):
# you can't read a file line-by-line and get line endings that match '\n\r'
# the following will match what your code is trying to do, but perhaps not
# what you want to accomplish..?
return f.read().split("\n\r") # are you sure you haven't switched these..?
def checked_read_file(fname):
try:
with open(fname, 'rb') as fp: # you'll probably need binary mode to read \r
return read_file(fp)
except IOError:
print("File does not exist")
return False
then you can write your while loop:
while True:
result = checked_read_file(user_file)
if result is not False: # this is correct since the empty list is false-y
break
user_file = input("Enter another filename: ") # or user_file = raw_input("...: ") if you're on Python 2
# here result is an array of lines from the file

Raising a FileNotFoundError

I have a function that takes a source file containing times (a csv file), reads it, then sorts the lines in order and writes them in a destination file. However, if the source csv file does not exist, I need to raise a FileNotFoundError. I've raised exceptions before, for example, if a parameter wasn't an integer I had to raise ChangeParameterError by using:
class ChangeParameterError(Exception):
pass
and then raising that in my function.
For my problem, my function is as follows:
def times(src,dst):
s = open(src,'r')
d = open(dst,'w')
lines = s.readlines()
s.close()
lines.sort()
for i in lines:
print((str(i).strip()), file = d)
d.close()
Any help is appreciated!
If the specified file is not found, the FileNotFoundError will be raised automatically by the open call when trying to open the file.
The exception is automatically raised by python. But you may need to wrap your open with a try-except to catch the exception without breaking your code:
try:
s = open(src,'r')
except FileNotFoundError:
print('file not found')

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')

Make python code continue after exception

I'm trying to read all files from a folder that matches a certain criteria. My program crashes once I have an exception raised. I am trying to continue even if there's an exception but it still stops executing.
This is what I get after a couple of seconds.
error <type 'exceptions.IOError'>
Here's my code
import os
path = 'Y:\\Files\\'
listing = os.listdir(path)
try:
for infile in listing:
if infile.startswith("ABC"):
fo = open(infile,"r")
for line in fo:
if line.startswith("REVIEW"):
print infile
fo.close()
except:
print "error "+str(IOError)
pass
Put your try/except structure more in-wards. Otherwise when you get an error, it will break all the loops.
Perhaps after the first for-loop, add the try/except. Then if an error is raised, it will continue with the next file.
for infile in listing:
try:
if infile.startswith("ABC"):
fo = open(infile,"r")
for line in fo:
if line.startswith("REVIEW"):
print infile
fo.close()
except:
pass
This is a perfect example of why you should use a with statement here to open files. When you open the file using open(), but an error is catched, the file will remain open forever. Now is better than never.
for infile in listing:
try:
if infile.startswith("ABC"):
with open(infile,"r") as fo
for line in fo:
if line.startswith("REVIEW"):
print infile
except:
pass
Now if an error is caught, the file will be closed, as that is what the with statement does.
Move the try/except inside the for loop.
Like in:
import os
path = 'C:\\'
listing = os.listdir(path)
for infile in listing:
try:
if infile.startswith("ABC"):
fo = open(infile,"r")
for line in fo:
if line.startswith("REVIEW"):
print infile
fo.close()
except:
print "error "+str(IOError)
You're code is doing exactly what you're telling it to do. When you get an exception, it jumps down to this section:
except:
print "error "+str(IOError)
pass
Since there's nothing after that, the program ends.
Also, that pass is superfluous.
As per strictest interpretation of the question "continue even if there's an exception". Python gives us a keyword "finally" which executes a block of code no matter what precedes it. The only issue with this method will run a block of code regardless of the type of error, which might not be desirable for all cases.
try:
unreal = 3/0 # raises divide by zero exception
print(unreal)
# handles zerodivision exception
except :
print("Can't divide by zero, 0 has no multiplicative inverse")
finally:
# this block is always executed
print("Brahmagupta claimed that “zero divided by a zero is zero.”)

Categories

Resources