Save results to a txt file python - python

I want to save these email results to my results.txt file in the directory.
def parseAddress():
try:
website = urllib2.urlopen(getAddress())
html = website.read()
addys = re.findall('''[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?''', html, flags=re.IGNORECASE)
print addys
except urllib2.HTTPError, err:
print "Cannot retrieve URL: HTTP Error Code: ", err.code
except urllib2.URLError, err:
print "Cannot retrive URL: " + err.reason[1]
# need to write the addys data to results.txt
with open('results.txt', 'w') as f:
result_line = f.writelines(addys)

Use return addys at the end of your function. print will only output to your screen.
In order to retrieve addys, you would need to call the function in your with statement or create a variable that contains the result of parseAddress().
You can save the memory that a variable would use by simply calling the function, like so:
with open('results.txt', 'w') as f:
f.write ( parseAddress() )

You mistakenly indented the "with" statement one space. This makes it subjective to an earlier block. I would think any self-respecting Python interpreter would flag this as not matching any earlier indentation, but it seems to be fouling your output.
Also, please consider adding some tracing print statements to see where your code did execute. That output alone can often show you the problem, or lead us to it. You should always provide actual output for us, rather than just a general description.

You need to fix your indentation, which is important in Python as it is the only way to define a block of code.
You also have too many statements in your try block.
def parseAddress():
website = None
try:
website = urllib2.urlopen(getAddress())
except urllib2.HTTPError, err:
print "Cannot retrieve URL: HTTP Error Code: ", err.code
except urllib2.URLError, err:
print "Cannot retrive URL: " + err.reason[1]
if website is not None:
html = website.read()
addys = re.findall('''[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?''', html, flags=re.IGNORECASE)
print addys
# need to write the addys data to results.txt
with open('results.txt', 'w') as f:
result_line = f.writelines(addys)

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

what is an exception handler for

I have a script which wants to load integers from a text file. If the file does not exist I want the user to be able to browse for a different file (or the same file in a different location, I have UI implementation for that).
What I don't get is what the purpose of Exception handling, or catching exceptions is. From what I have read it seems to be something you can use to log errors, but if an input is needed catching the exception won't fix that. I am wondering if a while loop in the except block is the approach to use (or don't use the try/except for loading a file)?
with open(myfile, 'r') as f:
try:
with open(myfile, 'r') as f:
contents = f.read()
print("From text file : ", contents)
except FileNotFoundError as Ex:
print(Ex)
You need to use to while loop and use a variable to verify in the file is found or not, if not found, set in the input the name of the file and read again and so on:
filenotfound = True
file_path = myfile
while filenotfound:
try:
with open(file_path, 'r') as f:
contents = f.read()
print("From text file : ", contents)
filenotfound = False
except FileNotFoundError as Ex:
file_path = str(input())
filenotfound = True

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"

with open(file) in except

In a try...except block i want to log the Exception error message to a file in the except path.
try:
doc = etree.parse(urllib2.urlopen(url))
except Exception, e:
print '%s: %s' % (e, url)
with open('error.txt', 'a') as f:
f.write('%s:%s\n' % url, e)
return
The print shows the error, but the with open ... f.write is not excecuted.
in the same script the relaxng validation is written to file
if not RELAXNG.validate(doc):
with open('error.txt', 'a') as f:
f.write('%s\n' % RELAXNG.error_log)
return
Can somebody explain to me, why
with open('myfile.txt', 'a') as f
f.write( ...
is posible in the if statement, but not in an except?
write() does not accept multiple arguments; you are probably missing parenthesis:
f.write('%s:%s\n' % (url, e))
Other than that, use absolute paths, not relative, as you can easily write the file in an unexpected place otherwise.
File operations inside except works fine.
>>> try:
raise SyntaxError("Hello")
except Exception:
with open("in.txt") as f:
print "F"
F
It should work. Try this for example:
try:
raise Exception
except Exception:
with open('error.txt', 'a') as f:
f.write('foobar')
If you run the above you will see foobar been written to file.
Is error.txt writeable by your process? There is some reason it's not writing to your file but it's not because the 'with' file context isn't allowed inside an except block.

Python shell freezes on reading (fasta) file

I am going to start of by showing the code I have thus far:
def err(em):
print(em)
exit
def rF(f):
s = ""
try:
fh = open(f, 'r')
except IOError:
e = "Could not open the file: " + f
err(e)
try:
with fh as ff:
next(ff)
for l in ff:
if ">" in l:
next(ff)
else:
s += l.replace('\n','').replace('\t','').replace('\r','')
except:
e = "Unknown Exception"
err(e)
fh.close()
return s
For some reason the python shell (I am using 3.2.2) freezes up whenever I tried to read a file by typing:
rF("mycobacterium_bovis.fasta")
The conditionals in the rF function are to prevent reading each line that starts with a ">" token. These lines aren't DNA/RNA code (which is what I am trying to read from these files) and should be ignored.
I hope anyone can help me out with this, I don't see my error.
As per the usual, MANY thanks in advance!
EDIT:
*The problem persists!*
This is the code I now use, I removed the error handling which was a fancy addition anyway, still the shell freezes whenever attempting to read a file. This is my code now:
def rF(f):
s = ""
try:
fh = open(f, 'r')
except IOError:
print("Err")
try:
with fh as ff:
next(ff)
for l in ff:
if ">" in l:
next(ff)
else:
s += l.replace('\n','').replace('\t','').replace('\r','')
except:
print("Err")
fh.close()
return s
You didn't ever define e.
So you'll get a NameError that is being hidden by the naked except:.
This is why it is good and healthy to specify the exception, e.g.:
try:
print(e)
except NameError as e:
print(e)
In cases like yours, though, when you don't necessarily know what the exception will be you should at least use this method of displaying information about the error:
import sys
try:
print(e)
except: # catch *all* exceptions
e = sys.exc_info()[1]
print(e)
Which, using the original code you posted, would have printed the following:
name 'e' is not defined
Edit based on updated information:
Concatenating a string like that is going to be quite slow if you have a large file.
Consider instead writing the filtered information to another file, e.g.:
def rF(f):
with open(f,'r') as fin, open('outfile','w') as fou:
next(fin)
for l in fin:
if ">" in l:
next(fin)
else:
fou.write(l.replace('\n','').replace('\t','').replace('\r',''))
I have tested that the above code works on a FASTA file based on the format specification listed here: http://en.wikipedia.org/wiki/FASTA_format using Python 3.2.2 [GCC 4.6.1] on linux2.
A couple of recommendations:
Start small. Get a simple piece working then add a step.
Add print() statements at trouble spots.
Also, consider including more information about the contents of the file you're attempting to parse. That may make it easier for us to help.

Categories