带有“with"的 Python 无效语法陈述 [英] Python invalid syntax with "with" statement

查看:33
本文介绍了带有“with"的 Python 无效语法陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 linux (maemo) 编写一个简单的 python 应用程序.但是我在第 23 行收到 SyntaxError: invalid syntax: with open(file,'w') as fileh:

I am working on writing a simple python application for linux (maemo). However I am getting SyntaxError: invalid syntax on line 23: with open(file,'w') as fileh:

代码可以在这里看到:http://pastebin.com/MPxfrsAp

我不知道我的代码有什么问题,我是 python 和with"语句的新手.那么,是什么导致此代码出错,我该如何解决?with"语句有问题吗?

I can not figure out what is wrong with my code, I am new to python and the "with" statement. So, what is causing this code to error, and how can I fix it? Is it something wrong with the "with" statement?

谢谢!

推荐答案

很可能,您使用的是不支持 with 语句的早期 Python 版本.下面是如何在不使用 with 的情况下做同样的事情:

Most likely, you are using an earlier version of Python that doesn't support the with statement. Here's how to do the same thing without using with:

fileh = open(file, 'w')
try:
    # Do things with fileh here
finally:
    fileh.close()

这篇关于带有“with"的 Python 无效语法陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆