import sys
try:
file = open("words.txt")
expect(IOError):
if file:
print "%s" % file
else:
print "Cant the %s file" % "words.txt"
this gives me an a error -
File "main.py", line 4
expect(IOError):
SyntaxError: invaild syntax
What im going wrong/ how do you fix this
From stackoverflow
-
Actually, it is except as in exception:
For instance:
except IOError: print "Error opening file!"
-
It's
except
. Read this. -
I think you're looking for except. The error handling part of the python tutorial explains it well.
-John
-
I assume you are trying to handle exceptions. In that case, use except, not expect. In any case except is not a function, rather it precedes a block of error handling code. When using files, you may want to look at the with statement and try-except-finally. The correction to your code is-
import sys try: file = open("words.txt") except IOError: #Handle error pass if file: print "%s" % file else: print "Cant the %s file" % "words.txt"
I hope this helps.
-
>>> try: ... f = open('words.txt') ... except IOError: ... print "Cant the %s file" % "words.txt" ... else: ... print "%s" % f
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.