[英]How to program a python file to use terminal options on mac
I am building a python file and instead of asking for user input I want it to get the information on the execution command. 我正在构建一个python文件,而不是要求用户输入,而是希望它获取有关执行命令的信息。 For example, I want to be able to type in
例如,我希望能够输入
python code.py -i "information"
Instead of having to execute code.py and then tell the code "information". 不必执行code.py,然后告诉代码“信息”。 How do I do this?
我该怎么做呢? PS I am using MacOS mojave as the tags indicate.
PS我正在使用MacOS mojave,如标签所示。 Thanks.
谢谢。
You can utilize sys.argv
in your Python script to read command-line arguments and store them in a list of strings that you can then parse, as follows: 您可以在Python脚本中利用
sys.argv
来读取命令行参数,并将其存储在字符串列表中,然后可以对其进行解析,如下所示:
from __future__ import print_function
import sys
print(sys.argv)
Then, from the terminal: 然后,从终端:
> python code.py -i "foo bar"
['code.py', '-i', 'foo bar']
You can use python's argparse
module to use command line arguments. 您可以使用python的
argparse
模块来使用命令行参数。 https://docs.python.org/3/library/argparse.html https://docs.python.org/3/library/argparse.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.