在 python 中捕获 IOError

发布于 2024-10-15 08:49:58 字数 476 浏览 8 评论 0原文

我编写了一个方法来执行一些操作并捕获错误的文件名。应该发生的情况是,如果路径不存在,则会抛出 IOError。但是,它认为我的异常处理语法不好......为什么?

def whatever():
    try:
        # do stuff
        # and more stuff
    except IOError:
        # do this
        pass
whatever()

但在调用 whatever() 之前,它会打印以下内容:

Traceback (most recent call last):
  File "", line 1, in 
  File "getquizzed.py", line 55
    except IOError:
         ^
SyntaxError: invalid syntax

when imported...help?!

I wrote a method that does some stuff and catches bad filenames. what should happen is if the path doesn't exist, it throws an IOError. however, it thinks my exception handling is bad syntax... why??

def whatever():
    try:
        # do stuff
        # and more stuff
    except IOError:
        # do this
        pass
whatever()

but before it even gets to calling whatever(), it prints the following:

Traceback (most recent call last):
  File "", line 1, in 
  File "getquizzed.py", line 55
    except IOError:
         ^
SyntaxError: invalid syntax

when imported...help?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时常饿 2024-10-22 08:49:59

只是在您的 try 块中缺少某些内容,即 pass 或任何内容,否则会出现缩进错误。

Just missing something in your try block, i.e. pass or anything, otherwise it gives an indentation error.

享受孤独 2024-10-22 08:49:59

如果您不将某些内容放入 try 块中,您将收到语法错误。
您可以输入 pass 只是为了保留空格:

try:
    # do stuff
    # and more stuff
    pass
except IOError:
    # do this
    pass

You will get a syntax error if you don't put something in side the try block.
You can put pass just to hold the space:

try:
    # do stuff
    # and more stuff
    pass
except IOError:
    # do this
    pass
指尖凝香 2024-10-22 08:49:58

检查你的缩进。这个无用的 SyntaxError 错误有 之前骗过我。:)

来自已删除的问题:

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

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
ㄖ落Θ余辉 2024-10-22 08:49:58

如果您有幸拥有较旧的安装,还有另外一种可能

您正在使用“as”语法:

     除了 IOError as ioe:

解析器被“as”绊倒了。

使用 as 是 Python 2.6 及更高版本中的首选语法。

这是 Python 2.5 及更早版本中的语法错误。对于 2.6 之前的版本,请使用:

     IOError 除外,ioe:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文