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使用zip合并相邻列表项的方法示例

本文实例讲述了Python使用zip合并相邻列表项的方法。分享给大家供大家参考,具体如下: 1》使用zip()函数和iter()函数,来合并相邻的列表项 >>> x...

跟老齐学Python之折腾一下目录

python在安装的时候,就自带了很多模块,我们把这些模块称之为标准库,其中,有一个是使用频率比较高的,就是 os 。这个库中方法和属性众多,有兴趣的看官可以参考官方文档:https:/...

python为tornado添加recaptcha验证码功能

复制代码 代码如下:    from urllib.request import urlopen    from urllib...

python使用writerows写csv文件产生多余空行的处理方法

初次接触python,学艺不精,第一次实战写一个文本处理的小程序时便遇到了头疼的问题。 先看代码: 生成的.CSV文件每两行之间都会多出一行空格(如下图),具体原因可参看点击打开链接...

python 实现视频流下载保存MP4的方法

如下所示: # -*- coding:utf-8 -*- import sys import os from glob import glob import requests...