Python 3中execfile的替代方案? [英] Alternative to execfile in Python 3?
本文介绍了Python 3中execfile的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python 2具有内置函数 execfile
,已在Python 3.0中删除。 这个问题讨论了Python 3.0的替代方案,但有些相当大的变化已经成为自Python 3.0以来。
Python 2 had the builtin function execfile
, which was removed in Python 3.0. This question discusses alternatives for Python 3.0, but some considerable changes have been made since Python 3.0.
execfile $的最佳替代方法是什么? Python 3.2的c $ c>
和未来的Python 3.x版本?
推荐答案
2to3
脚本(也是Python中的脚本) 3.2)替换
The 2to3
script (also the one in Python 3.2) replaces
execfile(filename, globals, locals)
按
exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals)
这似乎是官方推荐。
这篇关于Python 3中execfile的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文