Getting exception syntax error - python

I wrote a function. Now I keep getting syntax errors within the try statement. I don't know if its the code I wrote or the try statement
Function:
def connector (links):
for links in infile:
avenues = links.rstrip()
words = []
dct = {}
cord = []
There is more to the code but the error keeps occurring in the try statement, where it says except, any ideas?
try:
infile = open("routes.txt", "r")
links = inf.readlines()
Connector(links)
except LookupError as exceptObj:
print("Error:", str(exceptObj))

connector should be lowercase
You indented wrong
try:
infile = open("routes.txt", "r")
links = inf.readlines()
connector(links)
except LookupError as exceptObj:
print("Error:", str(exceptObj))

Related

Catch all errors when trying to open a txt file

I'd like to catch all errors when trying to open a txt file that doesn't even exist.
This is my code block:
def read_credentials(filename):
try:
with open(txt_file) as file:
lines = file.readlines()
username = lines[0].replace(" ", "").replace("\n", "")
password = lines[1].replace(" ", "").replace("\n", "")
return username, password
except IOError:
print("oops")
read_credentials('login.txt')
But I still get the following error:
username, password = read_credentials('login.txt')
TypeError: cannot unpack non-iterable NoneType object
How can I catch this?
I fixed it by wrapping the called function in an if-block:
if read_credentials('login.txt') is None:
print('oops')
else:
print('rest of code')

Why is error handling not working for IndexError?

I'm using a standard try/except syntax for skipping rows in a csv file that aren't streaming properly and therefore can't be downloaded. My code:
for row in list_reader:
media_id = row['mediaId']
filename = row['mediaId']
saveFile = media.get_item(media_id)
stream_url = saveFile['streams'][0]['streamLocation']
try:
r = requests.get(stream_url, allow_redirects=True)
with open(os.path.join('./media', filename), 'wb') as ofile:
ofile.write(r.content)
counter += 1
except:
IndexError
print "error"
However after downloading a number of files the problem row comes up, the error is not handled and I get the error:
Traceback (most recent call last):
File "downloadmedia.py", line 28, in <module>
stream_url = saveFile['streams'][0]['streamLocation']
IndexError: list index out of range
I've tried an if/else syntax instead, using the length of the stream_url variable, but this gives the same error. Can someone explain why the error handling doesn't work?
As stated in the comments, your try/except is in the wrong place. Through the error you provided, you can see that the index error occurs at the line stream_url = saveFile['streams'][0]['streamLocation']
You need to make sure the try/except is covering this line to prevent this.
for row in list_reader:
try:
media_id = row['mediaId']
filename = row['mediaId']
saveFile = media.get_item(media_id)
stream_url = saveFile['streams'][0]['streamLocation']
r = requests.get(stream_url, allow_redirects=True)
with open(os.path.join('./media', filename), 'wb') as ofile:
ofile.write(r.content)
counter += 1
except IndexError:
print "error"

Iterating the content of a text file in python

I have a text file named 'triple_response.txt' which contain the some text as :
(1,(db_name,string),DSP)
(1,(rel, id),2)
(2,(rel_name, string),DataSource)
(2,(tuple, id),201)
(2,(tuple, id),202)
(2,(tuple, id),203)
(201,(src_id,varchar),Pos201510070)
(201,(src_name,varchar),Postgres)
(201,(password,varchar),root)
(201,(host,varchar),localhost)
(201,(created_date,date),2015-10-07)
(201,(user_name,varchar),postgres)
(201,(src_type,varchar),Structured)
(201,(db_name,varchar),postgres)
(201,(port,numeric),None)
(202,(src_id,varchar),pos201510060)
(202,(src_name,varchar),Postgres)
(202,(password,varchar),root)
(202,(host,varchar),localhost)
(202,(created_date,date),2015-10-06)
(202,(user_name,varchar),postgres)
(202,(src_type,varchar),Structured)
(202,(db_name,varchar),DSP)
(202,(port,numeric),5432)
(203,(src_id,varchar),pos201510060)
(203,(src_name,varchar),Postgres)
(203,(password,varchar),root)
(203,(host,varchar),localhost)
(203,(created_date,date),2015-10-06)
(203,(user_name,varchar),postgres)
(203,(src_type,varchar),Structured)
(203,(db_name,varchar),maindb)
(203,(port,numeric),5432)
I am trying to convert these contents into JSON using a python script:
import re
import collections
import json, jsonpickle
def convertToJSON(File):
word_list=[]
row_list = []
try:
with open(File,'r') as f:
for word in f:
word_list.append(word)
with open(File,'r+') as f:
for row in f:
print row
row_list.append(row.split())
column_list = zip(*row_list)
except IOError:
print "Error in opening file.."
triple =""
for t in word_list:
triple+=t
tripleList = re.findall(r"\([^\(^\)]*\)",triple)
idList = re.split(r"\([^\(^\)]*\)",triple)
i =0
jsonDummy = []
jsonData = {}
for trip in tripleList:
nameAndType = re.split(r",|:",trip)
if(i==0):
key = re.compile("[^\w']|_").sub("",idList[i])
else:
try:
key = re.compile("[^\w']|_").sub("",idList[i].split("(")[1])
except IndexError:
pass
i = i+1
if(idList[i].find('(')!=-1):
try:
content = re.compile("[^\w']|_").sub("",idList[i].split(")")[0])
except IndexError:
pass
else:
content = re.compile("[^\w']|_").sub("",idList[i])
try:
trip = trip[1:-1]
tripKey = trip[1]
except IndexError:
tripKey = ''
name = re.compile("[^\w']").sub("",nameAndType[0])
try:
typeName = re.compile("[^\w']|_").sub("",nameAndType[1])
except IndexError:
typeName = 'String'
tripDict = dict()
value = dict()
value[name] = content
tripDict[key]=value
jsonDummy.append(tripDict)
for j in jsonDummy:
for k,v in j.iteritems():
jsonData.setdefault(k, []).append(v)
data = dict()
data['data'] = jsonData
obj = {}
obj=jsonpickle.encode(data, unpicklable=False)
return obj
pass
I am calling this function convertToJSON() within the same file as:
print convertToJSON("triple_response.txt")
I am getting the output as i expect like:
{"data": {"1": [{"db_name": "DSP"}, {"rel": "2"}], "201": [{"src_id": "Pos201510070"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151007"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "postgres"}, {"port": "None"}], "203": [{"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "maindb"}, {"port": "5432"}], "2": [{"rel_name": "DataSource"}, {"tuple": "201"}, {"tuple": "202"}, {"tuple": "203"}], "202": [{"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "DSP"}, {"port": "5432"}]}}
Now the problem which i am facing, which i am calling this from the outside the class as:
def extractConvertData(self):
triple_response = SPO(source, db_name, table_name, response)
try:
_triple_file = open('triple_response.txt','w+')
_triple_file.write(triple_response)
print "written data in file.."
with open('triple_response.txt','r+') as f:
for word in f:
print word
jsonData = convertToJSON(str('triple_response.txt'))
except IOError:
print "Not able to open a file"
print "Converted into JSON"
print jsonData
pass
The same code of convertToJSON() is not working...
It neither giving any output nor giving any error, it is not able to read the content from the 'triple_response.txt' file in the line.
with open('triple_response.txt','r+') as f:
for word in f:
print word
Any one can tell me solution to this problem..
_triple_file is never closed (except implicitly when you end the Python process, which is a terrible practice).
You can get platform-specific behavior when you have dangling filehandles like that (what is your platform? Unix? Windows?). Probably the write to _triple_file is not getting flushed.
So don't leave it dangling. Make sure to close it after you write it: (_triple_file.write(triple_response)). And in fact then assert that that file length is non-zero, using os.stat(), otherwise raise an Exception.
Also, you only have one big try...except clause to catch all errors, this is too much in one bite. Break it into two separate try...except clauses for writing _triple_file, and then reading it back. (Btw you might like to use tempfile library instead, to sidestep needing to know your intermediate file's pathname).
Something like the following untested pseudocode:
triple_response = SPO(source, db_name, table_name, response)
try:
_triple_file = open('triple_response.txt','w+')
_triple_file.write(triple_response)
_triple_file.close()
except IOError:
print "Not able to write intermediate JSON file"
raise
assert [suitable expression involving os.stat('triple_response.txt') to test size > 0 ], "Error: intermediate JSON file was empty"
try:
with open('triple_response.txt','r+') as f:
for word in f:
print word
jsonData = convertToJSON(str('triple_response.txt'))
except IOError:
print "Not able to read back intermediate JSON file"
#raise # if you want to reraise the exception
...

Python code to ignore errors

I have a code that stops running each time there is an error.
Is there a way to add a code to the script which will ignore all errors and keep running the script until completion?
Below is the code:
import sys
import tldextract
def main(argv):
in_file = argv[1]
f = open(in_file,'r')
urlList = f.readlines()
f.close()
destList = []
for i in urlList:
print i
str0 = i
for ch in ['\n','\r']:
if ch in str0:
str0 = str0.replace(ch,'')
str1 = str(tldextract.extract(str0))
str2 = i.replace('\n','') + str1.replace("ExtractResult",":")+'\n'
destList.append(str2)
f = open('destFile.txt','w')
for i in destList:
f.write(i)
f.close()
print "Completed successfully:"
if __name__== "__main__":
main(sys.argv)
Many thanks
You should always 'try' to open files. This way you can manage exceptions, if the file does not exist for example. Take a loot at Python Tutorial Exeption Handling
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise
or
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except IOError:
print 'cannot open', arg
else:
print arg, 'has', len(f.readlines()), 'lines'
f.close()
Do not(!) just 'pass' in the exception block. This will(!) make you fall on your face even harder.
Where ever your error(s) is happening you can wrap it in a try/except block
for i in loop:
try:
code goes here...
except:
pass

UnboundLocalError variable not recognized

I am getting an error that says: UnboundLocalError: local variable 'words' referenced before assignment but I am unsure on why. The following is my code:
def hasher(fname):
try:
with open(fname, 'r') as f:
words = re.split('(["\'#&,;:\(\)\s+\*\?\.]|\w+)', f.read().lower())
except:
print 'Out'
while '' in words:
words.remove('')
But I'm getting the error when I try to reference words in the while statement and I'm not sure on why. Any help? Thanks!
You need to define a default value,
def hasher(fname):
words = []
try:
with open(fname, 'r') as f:
words = re.split('(["\'#&,;:\(\)\s+\*\?\.]|\w+)', f.read().lower())
except:
print 'Out'
while '' in words:
words.remove('')
return words

Categories

Resources