python sys.argv[]用法实例详解
作者:快递小可 时间:2023-10-15 17:21:55
sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始,以下两个例子说明:
1、使用sys.argv[]的一简单实例:
以下是sample1.py文件:
import sys,os
print sys.argv
os.system(sys.argv[1])
这个例子os.system接收命令行参数,运行参数指令,cmd命令行带参数运行python sample1.py notepad,将打开记事本程序。
2、这个例子是简明python教程上的,明白它之后你就明白sys.argv[]了。
以下是sample.py文件:
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import sys
def readfile(filename): #定义readfile函数,从文件中读出文件内容
'''''''''Print a file to the standard output.'''
f = file(filename)
while True:
line = f.readline()
if len(line) == 0:
break
print line, # notice comma 分别输出每行内容
f.close()
# Script starts from here
print sys.argv
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
# fetch sys.argv[1] but without the first two characters
if option == 'version': #当命令行参数为-- version,显示版本号
print 'Version 1.2'
elif option == 'help': #当命令行参数为--help时,显示相关帮助内容
print '''
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help'''
else:
print 'Unknown option.'
sys.exit()
else:
for filename in sys.argv[1:]: #当参数为文件名时,传入readfile,读出其内容
readfile(filename)
在与sample.py同一目录下,新建3个记事本文件test.txt,test1.txt,test2.txt,内容如下图:
验证sample.py,如下:
C:\Users\91135\Desktop>python sample.py
['sample.py']
No action specified.
C:\Users\91135\Desktop>python sample.py --help
['sample.py', '--help']
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help
C:\Users\91135\Desktop>python sample.py --version
['sample.py', '--version']
Version 1.2
C:\Users\91135\Desktop>python sample.py --ok
['sample.py', '--ok']
Unknown option.
C:\Users\91135\Desktop>python sample.py test.txt
['sample.py', 'test.txt']
hello python!
C:\Users\91135\Desktop>python sample.py test.txt test1.txt test2.txt
['sample.py', 'test.txt', 'test1.txt', 'test2.txt']
hello python!
hello world!
hello wahaha!
goodbye!
C:\Users\91135\Desktop>
总结
以上所述是小编给大家介绍的python sys.argv[]用法实例详解网站的支持!
来源:https://blog.csdn.net/sxingming/article/details/52074311
标签:python,sys.argv,用法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Perl使用nginx FastCGI环境做WEB开发实例
2022-07-17 16:09:27
python使用rabbitmq实现网络爬虫示例
2022-04-20 20:11:58
go语言中fallthrough的用法说明
2024-04-23 09:33:47
![](https://img.aspxhome.com/file/2023/6/134416_0s.png)
深入浅析JavaScript函数前面的加号和叹号
2024-04-18 10:55:41
使用Python第三方库pygame写个贪吃蛇小游戏
2021-05-19 11:08:37
![](https://img.aspxhome.com/file/2023/1/76701_0s.png)
详解JavaScript 高阶函数
2024-04-18 09:30:14
详解用python实现爬取CSDN热门评论URL并存入redis
2022-08-30 00:11:35
![](https://img.aspxhome.com/file/2023/8/71458_0s.png)
用表格帮你了解Python数据类型
2023-11-08 08:55:25
Asp性能优化之Response.IsClientConnected属性及其应用示例
2008-09-18 12:13:00
Python挑选文件夹里宽大于300图片的方法
2023-08-13 11:35:19
python列表插入append(), extend(), insert()用法详解
2021-05-12 13:32:40
![](https://img.aspxhome.com/file/2023/4/93874_0s.png)
用python实现读取xlsx表格操作
2022-11-26 17:08:19
![](https://img.aspxhome.com/file/2023/7/103277_0s.png)
ORACLE数据库事务隔离级别介绍
2012-10-07 10:43:36
经验几则 推荐
2024-04-22 12:46:14
Pytorch 使用tensor特定条件判断索引
2023-01-18 16:30:23
![](https://img.aspxhome.com/file/2023/8/121158_0s.jpg)
php 无法载入mysql扩展
2023-09-07 13:07:40
部署.Net6项目到docker
2024-06-05 15:43:46
![](https://img.aspxhome.com/file/2023/5/122835_0s.jpg)
Python 语句的表达式和缩进
2023-11-26 18:28:24
ASP处理多关键词查询实例代码
2008-11-21 17:36:00
Python使用Asyncio进行web编程方法详解
2022-08-11 17:03:56
![](https://img.aspxhome.com/file/2023/8/70798_0s.png)