将命令行参数转换为 Python 中的标准输入
如何将接受命令行参数的脚本转换为能够通过标准用户输入接收参数?
目前我有一个文件,要处理它,我需要运行 python abc.py query 而我希望该文件能够通过 python abc.py 运行,然后在下一行中它应该要求用户通过 raw_input 输入,响应应该是传递给参数(即“查询”)。
How is it possible to convert a script accepting a command-line argument to be able to receive the argument through standard user input?
Currently I have a file and to process it I need to run python abc.py query
whereas I would like the file to be able to run through python abc.py
and then in the next line it should ask the user for input through raw_input
and the response should be passed to argument (i.e. 'query').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不改变
abc.py
本身,就无法实现这一点。如果您可以更改
abc.py
那么一个好方法是让它检查命令行参数,如果不存在则询问它......如下所示:There is no way to make this happen without changing
abc.py
itself.If you can change
abc.py
then a good way is to have it check for the command-line argument, and if it's not there then ask for it... something like this: