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

*号作为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

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

所以要小心




posted on 2015-01-30 17:53  代码王子  阅读(1784)  评论(0编辑  收藏  举报

导航