码迷,mamicode.com
首页 > 编程语言 > 详细

python脚本程序,传入参数*要用单引号'*'

时间:2015-01-30 19:42:54      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

*号作为python脚本的传入参数时,必须用单引号‘‘,才能正确传入。如python test.py 2014 ‘*‘ age

python test.py 2014 * age是错误的。



比如

test.py脚本如下

import sys
hdfs_report_historical_year = sys.argv[1]
# eg:2014-05,2014-12,etc.
hdfs_report_historical_month = sys.argv[2]
# eg:all,region,citylevel,etc.
pig_script_type = sys.argv[3]

print hdfs_report_historical_year
print hdfs_report_historical_month
print pig_script_type


其打印三个 传入参数。调用命令

python test.py 2014 * age

输出:

[wizad@srv26 new_pig]$ python test.py 2014 * age
2014
age.pig
appcategory.pig

可以看到参数* 没有正确传入,而是将当前目录下的文件名一起传给 sys.argv[2]和 sys.argv[3]。如果想正确传入要使用‘*‘:python test.py 2014 ‘*‘ age

我们可以看*不用单引号会传入很多参数,test.py增加参数打印如下

import sys
hdfs_report_historical_year = sys.argv[1]
# eg:2014-05,2014-12,etc.
hdfs_report_historical_month = sys.argv[2]
# eg:all,region,citylevel,etc.
pig_script_type = sys.argv[3]


print hdfs_report_historical_year
print hdfs_report_historical_month
print sys.argv[4] 
print sys.argv[5]
print sys.argv[6]
print sys.argv[7]
print sys.argv[8]

运行

[wizad@srv26 new_pig]$ python test.py 2014 * age  
2014
age.pig
appcategory.pig.backup
backup.py
citylevel.pig
config.py
config.pyc

可以看到,当前目录路径名基本上都被传入到脚本中。

所以要小心




python脚本程序,传入参数*要用单引号'*'

标签:

原文地址:http://blog.csdn.net/longshenlmj/article/details/43309617

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!