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

yipeiwu_com6年前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 Django连接MySQL数据库做增删改查

1、下载安装MySQLdb类库http://www.djangoproject.com/r/python-mysql/2、修改settings.py 配置数据属性复制代码 代码如下:DA...

Python函数的周期性执行实现方法

本文实例讲述了Python函数的周期性执行实现方法。分享给大家供大家参考,具体如下: 需要用到python的sched模块: #coding=utf-8 import time,sc...

python集合用法实例分析

本文实例讲述了python集合用法。分享给大家供大家参考。具体分析如下: # sets are unordered collections of unique hashable el...

用python拟合等角螺线的实现示例

用python拟合等角螺线的实现示例

人类很早就注意到飞蛾扑火这一奇怪的现象,并且自作主张地赋予了飞蛾扑火很多含义,引申出为了理想和追求义无反顾、不畏牺牲的精神。但是,这种引申和比喻,征求过飞蛾的意见吗? 后来,生物学家又提...

python3 破解 geetest(极验)的滑块验证码功能

下面一段代码给大家介绍python破解geetest 验证码功能,具体代码如下所示: from selenium import webdriver from selenium.web...