Check your indenting. This unhelpful SyntaxError error has fooled me before. :)
From the deleted question:
I'd expect this to be a duplicate, but I couldn't find it.
Here's Python code, expected outcome of which should be obvious:
x = {1: False, 2: True} # no 3
for v in [1,2,3]:
try:
print x[v]
except Exception, e:
print e
continue
I get the following exception: SyntaxError: 'continue' not properly in loop.
I'd like to know how to avoid this error, which doesn't seem to be
explained by the continue documentation.
I'm using Python 2.5.4 and 2.6.1 on Mac OS X, in Django.
Thank you for reading
Answer from Brian M. Hunt on Stack Overflowexception - catching an IOError in python - Stack Overflow
python - Difference between IOError and OSError? - Stack Overflow
python - what are the all of the "causes" for an IOerror - Stack Overflow
What’s the difference between IOError and OSError in python?
Check your indenting. This unhelpful SyntaxError error has fooled me before. :)
From the deleted question:
I'd expect this to be a duplicate, but I couldn't find it.
Here's Python code, expected outcome of which should be obvious:
x = {1: False, 2: True} # no 3
for v in [1,2,3]:
try:
print x[v]
except Exception, e:
print e
continue
I get the following exception: SyntaxError: 'continue' not properly in loop.
I'd like to know how to avoid this error, which doesn't seem to be
explained by the continue documentation.
I'm using Python 2.5.4 and 2.6.1 on Mac OS X, in Django.
Thank you for reading
there's 1 more possible if you're privileged to have an older installation
and
you're using the 'as' syntax:
except IOError as ioe:
and
the parser's getting tripped up on 'as'.
Using as is the preferred syntax in Python 2.6 and better.
It's a syntax error in Python 2.5 and older. For pre-2.6, use this:
except IOError, ioe:
There is very little difference between the two types. In fact, even the core Python developers agreed that there is no real difference and removed IOError in Python 3 (it is now an alias for OSError). See PEP 3151 - Reworking the OS and IO exception hierarchy:
While some of these distinctions can be explained by implementation considerations, they are often not very logical at a higher level. The line separating
OSErrorandIOError, for example, is often blurry. Consider the following:>>> os.remove("fff") Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [Errno 2] No such file or directory: 'fff' >>> open("fff") Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'fff'
Yes, that's two different exception types with the exact same error message.
For your own code, stick to throwing OSError. For existing functions, check the documentation (it should detail what you need to catch), but you can safely catch both:
try:
# ...
except (IOError, OSError):
# handle error
Quoting the PEP again:
In fact, it is hard to think of any situation where
OSErrorshould be caught but notIOError, or the reverse.
There's no difference between IOError and OSError cause they mostly appear on similar commands like opening a file or removing one.
According to the Python Docs, the IOError is an alias of the OSError. In other words, the IOError is actually the OSError, so let's take a look at what the Python docs say about the OSError:
This exception is raised when a system function returns a system-related error, including I/O failures such as “file not found” or “disk full” (not for illegal argument types or other incidental errors).
Note: The above statement is true for Python 3.3 and up.
Sources:
- Regarding the
IOErrorbeing an alias of theOSError: https://docs.python.org/3/library/exceptions.html#IOError
Note: If you click on the above link, you must scroll up a little to see the text talking about the IOError being an alias to the OSError.
- About the OSError: https://docs.python.org/3/library/exceptions.html#OSError
I/O errors aren't generated by Python, they are from the operating system. Python just parrots the error message passed from the OS.
Hello, I am trying to read from the .gz file and it is giving me the following error. Below are the code and traceback.
f = glob.glob (*/.gz)
if f:
for files in f:
with zgip.open(files, 'r') as r:
for line in r:
for i in range (3):
print lineTraceback:
Traceback (most recent call last):
File "tst.py", line 14, in <module>
for line in r:
File "python2.7/gzip.py", line 446, in readline
c = self.read(readsize)
File "python2.7/gzip.py", line 252, in read
self._read(readsize)
File "python2.7/gzip.py", line 287, in _read
self._read_gzip_header()
File "python2.7/gzip.py", line 181, in _read_gzip_header
raise IOError, 'Not a gzipped file'
IOError: Not a gzipped filePlease help me in solving this.