Python: Handle Missing Files from a Sequence - python

I have a program that's basically as follows:
for l in range(0,100):
file = open("C:/Twitter/json/user_" + str(l) + ".json", "r")
#do some stuff
file.close()
I am trying to figure out a way to handle the exception that will be thrown if say file 20 is missing, and tell it to continue. I attempted to use the continue with a try statement however, it kept complaining that I wasn't putting it in the loop properly. Any advice would be appreciated.
Basically I tried:
try:
for:
except:
continue
Thanks,

Something like :
import json
for l in xrange(100):
try:
with open('C:/Twitter/json/user_%d.json' % l, 'r') as f:
data = json.load(f)
#do stuff with obj
except IOError:
pass
edit fixed the code.

you could check file existence and then open it:
import os.path
os.path.exists(file_path)

Related

Unable to return the list that is read from a file(.csv)

I have been learning and practicing python and during which
I found one error in my program, but I'm unable to resolve. I want to return list of that is retrieved from a csv file. I tried the below code and it returns me an error.
import csv
def returnTheRowsInTheFile(fileName):
READ = 'r'
listOfRows = []
try:
with open(fileName, READ) as myFile:
listOfRows = csv.reader(myFile)
return listOfRows
except FileNotFoundError:
print('The file ' + fileName + ' is not found')
except:
print('Something went wrong')
finally:
#myFile.close()
print()
def main():
fullString = returnTheRowsInTheFile('ABBREVATIONS.CSV')
for eachRow in fullString:
print(eachRow)
return
main()
And the error is
Traceback (most recent call last): File
"C:\Users\santo\workspace\PyProject\hello\FinalChallenge.py", line 36,
in
main() File "C:\Users\santo\workspace\PyProject\hello\FinalChallenge.py", line 32,
in main
for eachRow in fullString: ValueError: I/O operation on closed file.
The easy way to solve this problem is to return a list from your function. I know you assigned listOfRows = [] but this was overwritten when you did listOfRows = csv.reader(myFile).
So, the easy solution is:
def returnTheRowsInTheFile(fileName):
READ = 'r'
try:
with open(fileName, READ) as myFile:
listOfRows = csv.reader(myFile)
return list(listOfRows) # convert to a list
except FileNotFoundError:
print('The file ' + fileName + ' is not found')
except:
print('Something went wrong')
You should also read pep8 which is the style guide for Python; in order to understand how to name your variables and functions.
When you use with open it closes the file when the context ends. Now listOfRows is of the return type of csv.Reader, and so is then fullString (not a list). You are trying to iterate on it, which seems to iterate over a file object, which is already closed.
As JulienD already pointed the file is alread closed when you try to read the rows from it. You can get rid of this exception using this for example:
with open(fileName, READ) as myFile:
listOfRows = csv.reader(myFile)
for row in listOfRows:
yield row
UPDATE
Btw the way you handle exceptions makes it pretty hard to debug. I'd suggest something like this.
except Exception as e:
print('Something went wrong: "%s"' e)
This way you can at least see the error message.

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

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.”)

display an error message when file is empty - proper way?

hi im slowly trying to learn the correct way to write python code. suppose i have a text file which i want to check if empty, what i want to happen is that the program immediately terminates and the console window displays an error message if indeed empty. so far what ive done is written below. please teach me the proper method on how one ought to handle this case:
import os
def main():
f1name = 'f1.txt'
f1Cont = open(f1name,'r')
if not f1Cont:
print '%s is an empty file' %f1name
os.system ('pause')
#other code
if __name__ == '__main__':
main()
There is no need to open() the file, just use os.stat().
>>> #create an empty file
>>> f=open('testfile','w')
>>> f.close()
>>> #open the empty file in read mode to prove that it doesn't raise IOError
>>> f=open('testfile','r')
>>> f.close()
>>> #get the size of the file
>>> import os
>>> import stat
>>> os.stat('testfile')[stat.ST_SIZE]
0L
>>>
The pythonic way to do this is:
try:
f = open(f1name, 'r')
except IOError as e:
# you can print the error here, e.g.
print(str(e))
Maybe a duplicate of this.
From the original answer:
import os
if (os.stat(f1name).st_size == 0)
print 'File is empty!'
If file open succeeds the value of 'f1Cont` will be a file object and will not be False (even if the file is empty).One way you can check if the file is empty (after a successful open) is :
if f1Cont.readlines():
print 'File is not empty'
else:
print 'File is empty'
Assuming you are going to read the file if it has data in it, I'd recommend opening it in append-update mode and seeing if the file position is zero. If so, there's no data in the file. Otherwise, we can read it.
with open("filename", "a+") as f:
if f.tell():
f.seek(0)
for line in f: # read the file
print line.rstrip()
else:
print "no data in file"
one can create a custom exception and handle that using a try and except block as below
class ContentNotFoundError(Exception):
pass
with open('your_filename','r') as f:
try:
content=f.read()
if not content:
raise ContentNotFoundError()
except ContentNotFoundError:
print("the file you are trying to open has no contents in it")
else:
print("content found")
print(content)
This code will print the content of the file given if found otherwise will print the message
the file you are trying to open has no contents in it

Categories

Resources