脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 接收处理外带的参数方法

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

2021-04-24 00:50枫奇 Python

今天小编就为大家分享一篇python 接收处理外带的参数方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

这个该怎么实现呢?

需要一个模块

?
1
from sys import argv

当然也可以直接只导入 sys

?
1
import sys

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

?
1
2
3
4
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])

结果如下:

?
1
2
3
4
5
6
7
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 接收处理外带的参数方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qiqiyingse/article/details/71123066

延伸 · 阅读

精彩推荐