Raising a FileNotFoundError - python

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

Related

failing to move file from input folder to error folder

I am trying to run a code where i am read files sheets. Issue i am facing is, if there is a file which has no sheet named SheetSum I am not able to move it to error location.
example:
def read_file(data_file):
# data_file = '\rr\ex.xlsx'
sheets = {}
try:
print("Reading file: "+data_file)
sheets['df_1'] = pd.read_excel(open(data_file,'rb'), 'SheetSum')
except Exception as excpt:
print("Exception occurred", exc_info=True)
return sheets
read_file(file)
shutil.move( file, dirpath +'\\processed_files')
Getting an error as:
[WinError 32] The process cannot access the file because it is being used by another process
Updates:
If finally is present, it specifies a cleanup handler. The try
clause is executed, including any except and else clauses. If an
exception occurs in any of the clauses and is not handled, the
exception is temporarily saved. The finally clause is executed. If
there is a saved exception it is re-raised at the end of the finally
clause. If the finally clause raises another exception, the saved
exception is set as the context of the new exception.
..More Here
Accepted Solution worked like charm.
Try closing the file before attempting to move it.
So something like
def read_file(data_file):
# data_file = '\rr\ex.xlsx'
sheets = {}
try:
print("Reading file: "+data_file)
sheets_file = open(data_file,'rb')
sheets['df_1'] = pd.read_excel(sheets_file, 'SheetSum')
except Exception as excpt:
print("Exception occurred", exc_info=True)
finally:
sheets_file.close()
return sheets
read_file(file)
shutil.move( file, dirpath +'\\processed_files')
The problem is the file opens, is handed to the pandas method, and when an exception occurs, your file is never closed.
Adding a finally to your try except block ensures that your file is always closed.

Python - close file and IOError

I'm writing a prog which writes data into a file, and each hour a new file is generated, with a precise title.
When the storage is full, it either stops the application, or deletes the oldest file created in the folder.
fp = open (fileNamePath, 'a')
fp.write('%s' % buffer)
try:
fp.close()
except IOError:
# delete file or close app
But when it comes to the except, it just skips it, and display IOError.
Thanks !
So if I understand it correctly, IOError is not catching exception but it is exploding. Did you try some more generic exception ('except Exception')?
Or you can try workaround with finally instead of except part:
finally:
if not f.closed:
# handle exception here
I looked for solution on Google, and someone suggested to use :
try:
with open(filePath,'a') as fp:
fp.write('%s' % buffer)
except IOError as exc:
# Handling exception
Well I don't get why but it works that way.
Thanks all !

What's the most clean and pythonic way to load data from one of two file paths? Why can't I catch two of the same Exceptions?

I have two potential file-paths where my application can read specific data from. If one fails, I want it to read from the other.
My intuitive attempt to do so was with try...except clauses, having something as below:
# Try the first file path
try:
file = open(possible_path_1)
content = file.read()
# File is not in first location, try the second
except IOError:
file = open(possible_path_2)
content = file.read()
# Could not read from either location, throw custom CriticalException
except IOError:
raise CriticalException("Could not read the file!")
However, this does not seem to work as intuitively expected. The second IOError is never caught. Why is this the case? Is there any "clean" way to read from one file path or the other without having to manually check os.path.exists(filepath) and os.path.isfile(filepath)?
Here's an alternative, but not sure if it is "prettier":
for path in paths:
try:
file = open(path)
content = file.read()
# File is not in first location, try the second
except IOError:
continue
break
else: # for-else gets executed if break never happens
raise CriticalException("Could not read the file!")
Assuming you have all your possible paths in some container, paths
Although honestly, I simply wouldn't use exception handling here, I think this is much clearer (and of course, I would use pathlib not os.path:
from pathlib import Path
for path in map(Path, paths):
if path.exists():
content = path.read_text()
break
else:
raise CriticalException("Could not read the file!")

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

Python with statement - is there a need for old-style file handling any more?

With having the with statement, is there ever a need to open a file/check for exceptions/do manual closing of resources, like in
try:
f = open('myfile.txt')
for line in f:
print line
except IOError:
print 'Could not open/read file'
finally:
f.close()
Your current code tries to handle the exception of the file not being found, or of insufficient access permissions etc., which a with open(file) as f: block wouldn't have done.
Also, in this case, the finally: block would have raised a NameError since f wouldn't have been defined.
In a with block, any exception (of whatever kind, maybe a division by zero in your code) that occurs within the block will still be raised, but even if you don't handle it, your file will always be closed properly. That's something entirely different.
What you want is probably:
try:
with open("myfile.txt") as f:
do_Stuff() # even if this raises an exception, f will be closed.
except IOError:
print "Couldn't open/read myfile.txt"

Categories

Resources