从 shell 运行 Python 程序并将变量保留在作用域内
Python 在解释器中有一个很好的 execfile 函数,您可以在其中运行程序,将所有变量保留在范围内,然后在闲暇时检查它们。但是,据我所知,您无法在从命令行获取参数的程序上运行 execfile ;如果您尝试包含参数,Python 会抛出 IOError
并抱怨无法找到该文件(带有空格和参数)。
有没有什么方法可以运行接受命令行参数的Python脚本,并在程序执行后将所有变量保留在作用域内?就像带有标志的 execfile
一样?
谢谢, 凯文
Python has a nice execfile
function inside the interpreter where you can run a program, keep all the variables in scope, and then inspect them at your leisure. However, as far as I know you can't run execfile
on a program that takes arguments from the command line; if you try to include the arguments, Python throws an IOError
and complains that the file (with spaces and arguments) can't be found.
Is there any way to run a Python script that takes command line arguments, and keep all of the variables in scope after the program executes? Like an execfile
that takes flags?
Thanks,
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以直接修改
sys.argv
。文件:另一个文件:
输出:
但我必须引用 Ignacio Vazquez-Abrams 的话:
我假设你有你的理由。
You could modify
sys.argv
directly. The file:The other file:
Output:
But I must say, quoting Ignacio Vazquez-Abrams:
I'm assuming you have your reasons though.