multiple try/except in a single function does not work - python

def creating_folder_for_csv_files(cwd):
try:
os.makedirs(cwd+'\\migration_data\\trade')
except os.error, e:
print "Could not create the destination folder for CSV files"
# end of first try/except block
try:
os.makedirs(cwd+'\\migration_data\\voucher')
except os.error, e:
print "Could not create the destination folder for CSV files"
In my code, the first try/except block works but the second does not. What's the problem?

The voucher might already exist.

Related

schedule task if exception has occurred python

my question is simple as the title suggest
try:
response = requests.get(URL2) # download the data behind the URL
open(zipname, "wb").write(response.content) # Open the response into a new file
# extract zip file to specified location
with ZipFile(zipname, 'r') as zip_file:
zip_file.extractall(path=path)
os.remove(zipname) # removes the downloaded zip file
print("itworks")
except (requests.exceptions.ConnectionError, FileNotFoundError):
print("finally the error")
# retry the try part after some seconds
now i want it to retry and go over again in case the exception happen, after some time.
FOA (Looking at the accepted answer) I wouldn't use recursion where it's not necessary for a whole bunch of reasons, among which readability, mantainability, and the very name of this platform.
Then I would exempt doSomething() from catching the exception and embed the try-catch block in a while loop, like so:
def doSomething():
"do something here"
while True:
try:
doSomething()
print("success")
break
except (requests.exceptions.ConnectionError, FileNotFoundError):
print("error, trying again in 10s")
time.sleep(10)
This does a better job at separating concerns; doSomething() just has to... do something. Error catching/logging can be handled outside.
You can always make it a recursive function and import time module to wait x seconds.
For instance:
import time
def doSomething():
try:
response = requests.get(URL2) # download the data behind the URL
open(zipname, "wb").write(response.content) # Open the response into a new file
# extract zip file to specified location
with ZipFile(zipname, 'r') as zip_file:
zip_file.extractall(path=path)
os.remove(zipname) # removes the downloaded zip file
print("itworks")
except (requests.exceptions.ConnectionError, FileNotFoundError):
print("finally the error")
# retry the try part after some seconds
time.sleep(1000)
# Try again
doSomething()
If I understand correctly you can do the following:
from time import sleep
no_of_attempts = 5 # set number of attempts
for i in range (no_of_attempts):
try:
response = requests.get(URL2) # download the data behind the URL
open(zipname, "wb").write(response.content) # Open the response into a new file
# extract zip file to specified location
with ZipFile(zipname, 'r') as zip_file:
zip_file.extractall(path=path)
os.remove(zipname) # removes the downloaded zip file
print("itworks")
break
except (requests.exceptions.ConnectionError, FileNotFoundError):
print("finally the error")
sleep(3)
continue
This way you can retry the "try" part as many times as you set no_of_attempts to be.
You could also set while True if you want it to try until is succeeds and then break inside the try but I would not recommend it

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

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

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.

Should I nest try...except blocks?

I want to store some data in a web. I want to do two operations: the first is to open a URL, the second is to store data, with both of them in try...except blocks.
I'd like to know if nesting try...except is good or not, and why.
Solution one:
try:
# open url
# store data
except:
# url doesn't exist
# cannot store
Solution two:
try:
# open url
try:
# store data
except:
# cannot store
except:
# cannot open url
As naiquevin suggested, it might be useful to catch exactly what you intend to:
try:
openURL()
except URLError:
print "cannot open URL"
else:
try:
saveData()
except IOError:
print "cannot save data"

python fnmatch unable to find the file

I have a directory that has bunch of sub directories, each subdir has many csv files, but I am only interest in certain csv file. So I wrote following python method, but I am unable to capture the file name, if I do *.csv it will find all the file but I don't want to all the files to be read in:
def gatherStats(template_file, csv_file):
for lang in getLanguageCodes(csv_file):
lang_dir = os.path.join(template_file, lang)
try:
for file in os.listdir(lang_dir):
if fnmatch.fnmatch(file, '*-*-template-users-data.csv'):
t_file = open(file, 'rb').read()
reader = csv.reader()
for row in reader:
print row
else:
print "didn't find the file"
except Exception, e:
logging.exception(e)
What am I doing wrong here? Is it a regular expression issue? Can we use regular expression with fnmath?
There are several problems with your code. Fix them first, then we might get to the bottom of what your issue really is.
First of all, don't use built-in names as variables, such as file. Rather replace it with filename.
Then os.path.join(lang_dir, filename) before opening the file. Meaning:
t_file = open(os.path.join(lang_dir, filename), 'rb').read()
How do you expect reader = csv.reader() to read your file if you don't reference your open file object in this line?
Your try/except block is a bit too wide for my taste. Take your time and narrow down the errors that actually can happen. Then decide which of them you want to ignore and which should crash your program. Take a close look at the exceptions actually thrown in this block. You'll probably find your issue there.
With the help provided by another user, I manage to fix the problem. I am putting this answer here for future reference for community.
def gatherStats(template_file, csv_file):
for lang in getLanguageCodes(csv_file):
lang_dir = os.path.join(template_file, lang)
try:
for filename in os.listdir(lang_dir):
path = os.path.join(lang_dir, filename)
if re.search(r'-.+-template-users-data.csv$',filename):
with open(path, 'rb') as template_user_data_file:
reader = csv.reader(template_user_data_file)
try:
for row in reader:
print row
except csv.ERROR as e:
logging.error(e)
else:
print "didn't find the file"
except Exception, e:
logging.exception(e)

Categories