使用 IDLE 将命令行参数传递给 Python 程序?
我已经下载了一个 python 文件 xxxxxx.py ,该文件应该在命令行上运行 输入:python xxxxxx.py filename1 filename2
并且应该将这两个文件作为参数。
我想知道是否有一种方法可以使用 IDLE 来传递这些参数。除了设置 sys.argv 之外还有其他方法吗?
谢谢
I have downloaded a python file xxxxxx.py that is supposed to run on the command line by
typing: python xxxxxx.py filename1 filename2
and that should take these two files as arguments.
I was wondering if there is a way I can use IDLE to pass in these arguments. Is there a way other than setting sys.argv
?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于 Python 文件的内容。如果它写得很好,比如:
那么你可以简单地导入 python 文件并运行它,比如:
所以,这实际上取决于它是如何编写的。如果写得不好但你可以编辑它,我会重构它,以便它可以用作库;否则,我将使用 subprocess 模块来调用该程序。
It depends on the content of your Python file. If it is well-written, like:
Then you could simply import the python file and run it like:
So, it really depends on how it is written. If it isn't written well but you can edit it, I would refactor it so that it can be used as a library; otherwise, I would use the subprocess module to invoke the program.
您可以从命令行执行此操作:
idle.py -r scriptname.py 在此处放置参数
您可以尝试不同的 IDE,例如 ActivePython
或者您可以修补 IDLE:
http://bugs.python.org/issue5680
You can do this from the command line with:
idle.py -r scriptname.py put arguments here
You can try a different IDE like ActivePython
Or you can patch IDLE:
http://bugs.python.org/issue5680