Exception handling for reading in csv - python

My program takes in input of the file name and makes some alterations to the file. In case the user enters the wrong name, I don't want my program to crash.
Currently, my code is:
try:
df = pd.read_csv('directory\\%s' % user_input)
except Exception:
[error message]
For some reason, I keep getting the error message even when the file name exists. Is something wrong with my approach?

Whenever possible you should try to be specific with your exception handling. Your code doesn't look quite right.
import os
try:
df = pd.read_csv(os.path.join(directory, user_input))
except IOError as e:
# print e
Might be what you want.

Your file path is formatted wrong.
'directory\\%s' % user_input

Related

Is there a way to prevent the "bad connection" or "no network" error inside python?

I use urlopen command from urllib.request package and it works properly.
But because it is inside an infinite loop I want to consider the possible "No network" Conditions. I do not want my code to break down because of this error.
I tried the function below but it does not work:
def update():
try:
cmd = 'getUpdates'
resp = urlopen(URL + cmd)
line = aux_dec2utf8(resp)
upds = json.loads(line)
NoM = len(upds['result'])
except ValueError:
print('NO NETWORK')
return NoM, upds
Error Image
I think the issue here is the ValueError exception. You may want to set up an exception for urllib.HTTPError and/or urllib.URLError to catch the error you’re attempting to keep from breaking your script.
ValueError is used to catch an invalid argument and may not be what’s killing the flow.

How adding the appropriate Except to a program when reading from a file in python?

I have been trying to capture an exception that returns the message "Error in file!" when reading from a file in Python.
I had managed to catch the error when the file isn't existing, but unfortunately I couldn't manage to capture the error when there is an error in a file.
Any suggestions?
try:
# ...
except IOError:
print("Error opening file!")
except Exception:
print("Error in file!")
raise
You can track the exception to find out what exactly went wrong in the try-block, so you can handle the new error.
In order to do this, just print details of the exception:
try:
....
except Exception as f:
print(f.__class__)
print(str(f))
This will give you the class of the error (e.g. NameError) and the specific error the program encountered. So, you can handle it with an additional except-clause or debug the code.

suggestions for to improve my python function to parse Wordpress/config.php

I am writing a python script function to backup Wordpress. As part of the script i wrote a function to fetch database details from the config.php file.
Working of my function
function takes Wordpress installation location as an argument and using regex to match db_user,db_host,db_user,db_password from that file, the function will exist if can not find "config.php". I am using sys.exit(1) to exit from the function is that the proper way to exit from a function? I am pasting my function code snippet.
def parsing_db_info(location):
config_path = os.path.normpath(location+'/config.php')
if os.path.exists(config_path):
try:
regex_db = r'define\(\s*?\'DB_NAME\'\s*?,\s*?\'(.*?)\'\s*?'.group(1)
regex_user = r'define\(\s*?\'DB_USER\'\s*?,\s*?\'(.*?)\'\s*?'.group(1)
regex_pass = r'define\(\s*?\'DB_PASSWORD\'\s*?,\s*?\'(.*?)\'\s*?'.group(1)
regex_host = r'define\(\s*?\'DB_HOST\'\s*?,\s*?\'(.*?)\'\s*?'.group(1)
db_name = re.match(regex_db,config_path).group(1)
db_user = re.match(regex_user,config_path).group(1)
db_pass = re.match(regex_pass,config_path).group(1)
db_host = re.match(regex_host,config_path).group(1)
return {'dbname':db_name , 'dbuser':db_user , 'dbpass':db_pass , 'dbhost':db_host}
except exception as ERROR:
print(ERROR)
sys.exit(1)
else:
print('Not Found:',config_path)
sys.exit(1)
AFTER EDITING
def parsing_db_info(location):
config_path = os.path.normpath(location+'/wp-config.php')
try:
with open(config_path) as fh:
content = fh.read()
regex_db = r'define\(\s*?\'DB_NAME\'\s*?,\s*?\'(.*?)\'\s*?'
regex_user = r'define\(\s*?\'DB_USER\'\s*?,\s*?\'(.*?)\'\s*?'
regex_pass = r'define\(\s*?\'DB_PASSWORD\'\s*?,\s*?\'(.*?)\'\s*?'
regex_host = r'define\(\s*?\'DB_HOST\'\s*?,\s*?\'(.*?)\'\s*?'
db_name = re.search(regex_db,content).group(1)
db_user = re.search(regex_user,content).group(1)
db_pass = re.search(regex_pass,content).group(1)
db_host = re.search(regex_host,content).group(1)
return {'dbname':db_name , 'dbuser':db_user , 'dbpass':db_pass , 'dbhost':db_host}
except FileNotFoundError:
print('File Not Found,',config_path)
sys.exit(1)
except PermissionError:
print('Unable To read Permission Denied,',config_path)
sys.exit(1)
except AttributeError:
print('Parsing Error wp-config seems to be corrupt,')
sys.exit(1)
To answer your question, you shouldn't normally use sys.exit inside a function like that. Rather, get it to raise an exception in the case where it fails. Preferably, it should be an exception detailing what went wrong, or you could just let the existing exceptions propagate.
The normal rule in Python is this: deal with exceptions at the place you know how to deal with them.
In your code, you catch an exception, and then don't know what to do, so call sys.exit. Instead of this, you should:
let an exception propagate up to a top-level function which can catch it, and then call sys.exit if appropriate
wrap the exception in something more specific, and re-raise, so that a higher level function will have a specific exception to catch. For example, your function might raise a custom ConfigFileNotFound exception or ConfigFileUnparseable exception.
Also, you have put except exception, you probably mean except Exception. However, this is extremely broad, and will mask other programming errors. Instead, catch the specific exception class you expect.

How to continue python script loop even though IOError?

I have a program which request info from the twitter API, and from time to time I get an error:
IOError: [Errno socket error] [Errno 54] Connection reset by peer
I want to know how can I keep my script running (looping). I know that this has something to do with:
try:
except IOError:
but I can not figure it out.
The simpler structure is like this:
my_while_or_for_loop:
some_code_here_maybe
try:
my_code_or_function_that_sometimes_fails()
except IOError:
pass # or some code to clean things that went wrong or to log the failure
some_more_code_here_maybe
You want to read the docs
The full construction can be more complex and includes try/except/else/finally.
From an example in docs:
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print "division by zero!"
... else:
... print "result is", result
... finally:
... print "executing finally clause"
Here is is documentationabout exceptions...
Simply, if a code block have possibility to cause some known errors (like input output error) in some conditions, you define an try-exceptblock to handle such errors. That will make your script keep runnung and let you execute diffrent code blocks according to diffrent error status.... Like:
try:
<do something>
except IOError:
<an input-output error occured, do this...>
except ValueError:
<we got something diffrent then we expected, do something diffrent>
except LookupError:
pass # we do not need to handle this one, so just kkeep going...
except:
<some diffrent error occured, do somethnig more diffrent>
If you simply do nothing and continue, you can use pass, like:
try:
<do something>
except:
pass
The part you're missing is pass. That is a simple no-op expression, which exists because Python can have no empty blocks.
The longer explanation:
What you need to do is to catch the IOError exception being thrown, and ignore it (possibly logging it and such) using pass.
To do that you need to wrap the code that can fail in a try and except block, like this:
try:
<code that can fail>
except IOError:
pass
What this does is explicitly ignore IOErrors, while not ignoring others. If you want to ignore all exceptions, simply remove the IOError part so the line says except:.
You should really read the Python tutorial, specifically the section about error handling.
Try this:
try:
<do something - you code here>
except IOError: pass
Or why not:
with ignored(IOError):
<code that can fail>

a question about this python script!

if __name__=="__main__":
fname= raw_input("Please enter your file:")
mTrue=1
Salaries=''
Salarieslist={}
Employeesdept=''
Employeesdeptlist={}
try:
f1=open(fname)
except:
mTrue=0
print 'The %s does not exist!'%fname
if mTrue==1:
ss=[]
for x in f1.readlines():
if 'Salaries' in x:
Salaries=x.strip()
elif 'Employees' in x:
Employeesdept=x.strip()
f1.close()
if Salaries and Employeesdept:
Salaries=Salaries.split('-')[1].strip().split(' ')
for d in Salaries:
s=d.strip().split(':')
Salarieslist[s[0]]=s[1]
Employeesdept=Employeesdept.split('-')[1].strip().split(' ')
for d in Employeesdept:
s=d.strip().split(':')
Employeesdeptlist[s[0]]=s[1]
print "1) what is the average salary in the company: %s "%Salarieslist['Avg']
print "2) what are the maximum and minimum salaries in the company: maximum:%s,minimum:%s "%(Salarieslist['Max'],Salarieslist['Min'])
print "3) How many employees are there in each department :IT:%s, Development:%s, Administration:%s"%(
Employeesdeptlist['IT'],Employeesdeptlist['Development'],Employeesdeptlist['Administration'])
else:
print 'The %s data is err!'%fname
When I enter a filename, but it didn't continue, why? If I enter a file named company.txt, but it always show the file does not exist. why?
I can give you some hints which can help you to resolve problem better
Create a function and call it in main e.g.
if __name__=="__main__":
main()
Don't put whole block under if mTrue==1: instead just return from function on error e.g.
def main():
fname= raw_input("Please enter your file:")
try:
f1=open(fname)
except:
print 'The %s does not exist!'%fname
return
... # main code here
Never catch all exceptions , instead catch specific exception e.g. IOError
try:
f1 = open(fname):
except IOError,e:
print 'The %s does not exist!'%fname
otherwise catching all exception may catch syntax error or mis-spelled names etc
Print the exception you are getting, it may not always be file not found, may be you don't have read permission or something like that
and finally your problem could be just that, file may not exist, try to input full path
Your current working directory does not contain company.txt.
Either set your current working directory or use an absolute path.
You can change the working directory like so:
import os
os.chdir(new_path)
In addition to be more specific about which exceptions you want to catch you should considered capturing the exception object itself so you can print a string representation of it as part of your error message:
try:
f1 = open(fname, 'r')
except IOError, e:
print >> sys.stderr, "Some error occurred while trying to open %s" % fname
print >> sys.stderr, e
(You can also learn more about specific types of Exception objects and perhaps handle
some sorts of exceptions in your code. You can even capture Exceptions for your own inspection from within the interpreter so you can run dir() on them, and type() on each of the interesting attributes you find ... and so on.

Categories

Resources