这里是什么,我基本上是试图做的事: import sh, os
with sh.cd('/tmp'):
print os.getcwd()
print os.getcwd()
我碰到下面的错误,虽然 line 3, in <module>
with sh.cd('/tmp'):
AttributeError: __exit__
缺少什么我在这里?是否有其他解决
为了说明我的问题,这里不使用contextlib一个基本要求:使用contextlib相同的请求 import urllib.request
url = "http://www.example.com/"
with urllib.request.urlopen(url) as response:
raw_data = response.read()
和: i
是否有可能有一个上下文管理器,只是保持以前的run执行状态。在代码: 编辑:不工作的解决方案,这是我预料 with sudo('. myapp'): #this runs a few things and sets many env variables
run('echo $ENV1') # $ENV1 isn't set because the sudo command ran ind
我想要的行为,例如: with A() as f:
for x in f:
do_something(f)
这是做正确的方式? class A:
def __enter__(self):
print "Entering context"
def __iter__(self):
for x in ["some","list"]: