2013-06-05 2 views
0

StringIO로 완료된 memfile을 실행하고 싶습니다. 그것을 할 가능성이 있습니까? 이런 일이 :python 실행중인 StringIO 파일

import StringIO 

memfile = StringIO.StringIO() 
memfile.write("print 'hello world'") 

#with diskfiles I would do: 
#os.system('python memfile') ? 
#subprocess.Popen('memfile', shell=True) 
memfile.close() 
+0

를 그리고 무엇을 것 메모리 파일을 실행해야합니까? 파이썬 코드, bash 스크립트, 바이너리 실행 파일? –

+0

'memfile'에 쓰여진 예제 내용은 파이썬 코드처럼 보입니다. – piokuc

+0

exec memfile.getvalue()로 해결했고, memfile 내용 "a = 0 \ nb = 1 \ nprint a + b "감사합니다 – user2455985

답변

0

이 작동합니다 :

eval(memfile.open().read()) 

편집 : eval는 표현을 받아들이으로

그것은, exec이 필요 밝혀 :

exec memfile 
+0

print가 expresion이 아니기 때문에 작동하지 않지만 실마리를주었습니다. exec memfile.getvalue() works, thanks! – user2455985

관련 문제