简体   繁体   English

如何获得Python程序以将参数作为来自UNIX终端的标准输入的输入?

[英]How do I get a Python program to take in parameters as inputs that come from standard input from the UNIX terminal?

For example - I want to do something like... 例如-我想做类似...

python DoublePendulum.py INPUT1 INPUT2

(where INPUT1 and INPUT2 are taken as variable inputs within the DoublePendulum program). (其中INPUT1INPUT2作为DoublePendulum程序中的变量输入)。

$ python test.py arg1 arg2 arg3

In test.py 在test.py中

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

output 输出

Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

Python also provided modules that helps you parse command-line options and arguments. Python还提供了有助于您解析命令行选项和参数的模块。 There are the following modules in the standard library: 标准库中包含以下模块:

  • The getopt module is similar to GNU getopt. getopt模块类似于GNU getopt。
  • The optparse module offers object-oriented command line option parsing, optparse was deprecated in version 2.7 of Python argparse is the replacement. optparse模块提供了面向对象的命令行选项解析,而python 2.7版中已弃用optparse,而argparse被替换。

$ python test.py one two five something

inside test.py 内部test.py

import sys
print(sys.argv[0:1], sys.argv[1:2], sys.argv[2:3], sys.argv[3:4], sys.argv[4:5])  

will output as lists: 将输出为列表:

 ['test.py'] ['one'] ['two'] ['five'] ['something'] 
for my_var in sys.argv:
    print(my_var)

will output: as strings 将输出:作为字符串

 test.py one two five something 

I made this function for that returns the parameter you want 我做了这个功能,返回您想要的参数

def give_me_arg(n):
    num = len(sys.argv)
    if n >= num:
        print("Only enter:>",num,'<-and are this from 0 to ',num-1,':', sys.argv)
        return ''
    else:
        for my_var in sys.argv[n:n+1]:
            return my_var

my_var=give_me_arg(3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同的终端向运行在linux终端窗口中的python程序的python程序提供raw_input,它们都在同一UNIX上运行 - How to supply raw_input to a python program running on a terminal window in linux from a different terminal, all running on same UNIX 如何从终端获取python脚本的输入文件? - How to take input file from terminal for python script? 如何从用户那里获取多个(大量)输入并在用户提供的每个输入中重复代码? (Python) - How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python) Python-如何让我的程序反复要求输入,但仍存储其他输入? - Python - How do I get my program to ask for an input repeatedly but still store the other inputs? 如何在 Python 中持续从控制台获取输入 - How do I persistently take input from the console in Python Python装饰器-参数从何而来? - Python decorators - Where do parameters come from? 如何使用 Python 从用户那里获得多个输入? - How do I get multiple inputs from the user with Python? 当我的 python 脚本正在运行时,如何阻止 Linux 将键输入写入终端? - How do I stop Linux from writing key inputs to the terminal while my python script is running? 我如何从Python执行此终端命令? - How do I get this terminal command to be executed from python? 如何在终端中运行 python 程序并让它显示错误? - How do I run a python program in terminal and get it to display errors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM