python 接收处理外带的参数方法

不要沮丧,不必惊慌,做努力爬的蜗牛或坚持飞的笨鸟,我们试着长大,一路跌跌撞撞,然后遍体鳞伤。坚持着,总有一天,你会站在最亮的地方,活成自己曾经渴望的模样。

在执行python 代码的时候,有时候需要传递外面的参数进行处理

这个该怎么实现呢?

需要一个模块

from sys import argv

当然也可以直接只导入 sys

import sys

然后使用的时候, 用sys.argv也是可行的

import sys
print "the script name is ", sys.argv[0]
for num in range(1, len(sys.argv)):
 print "parameter %d is %s "% (num, sys.argv[num])

结果如下:

python test2.py this is a test last_parameter_Success
the script name is test2.py
parameter 1 is this 
parameter 2 is is 
parameter 3 is a 
parameter 4 is test 
parameter 5 is last_parameter_Success 

以上这篇python 接收处理外带的参数方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战