python 2.4.3中的tempfile语法 [英] tempfile syntax in python 2.4.3

查看:152
本文介绍了python 2.4.3中的tempfile语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以在Python 2.6.6上完美运行:

I have the following code which runs perfectly on Python 2.6.6:

import tempfile
with tempfile.NamedTemporaryFile() as scriptfile:
        scriptfile.write(<variablename>)
        scriptfile.flush()
        subprocess.call(['/bin/bash', scriptfile.name])

但是,当我尝试在Python 2.4.3上运行它时,出现以下错误:

However, when I try to run it on Python 2.4.3, I get the following error:

File "<stdin>", line 2
    with tempfile.NamedTemporaryFile() as scriptfile
                ^
SyntaxError: invalid syntax

Python 2.4.3的语法是否有变化?

Is there a change in syntax in Python 2.4.3?

推荐答案

Python 2.4不支持with语句.因此,您只需要手动打开和关闭scriptfile.

Python 2.4 does not support the with statement. So you just need to open and close the scriptfile manually.

scriptfile = tempfile.NamedTemporaryFile()

# whatever you need to do with `scriptfile`

scriptfile.close()

这篇关于python 2.4.3中的tempfile语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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