运行python程序,不确定参数是什么写的?

时间:2015-11-26 03:29:23

标签: python python-2.7

所以我想运行这个python程序,但我不确定如何运行它,当我在参数中插入它得到一个令牌错误。

首先我"导入cw2"这是输入python后终端中的文件名,然后我输入参数来运行一个单独的任务,但我得到了错误。继承代码,你能告诉我如何运行各个部分。

下面是我输入的参数cw2 -u user_745409913574d4c6 -d doc_140228202800-6ef39a241f35301a9a42cd0ed21e5fb0 -t task_2,但这不起作用。下面的代码显示了参数是什么。

def main(argv):
user_uuid = ''
doc_uuid = ''
task_id = 0
try:
    opts, args = getopt.getopt(argv, "hu:d:t:", ["user_uuid=", "doc_uuid=", "task_id="])
except getopt.GetoptError:
    print 'cw2 -u <user_uuid> -d <doc_uuid> -t <task_id>'
    sys.exit(2)
for opt, arg in opts:
    if opt == '-h':
        print 'cw2.py -u <user_uuid> -d <doc_uuid> -t <task_id>'
        sys.exit()
    elif opt in ("-u", "--user_uuid"):
        user_uuid = arg
    elif opt in ("-d", "--doc_uuid"):
        doc_uuid = arg
    elif opt in ("-t", "--task_id"):
        task_id = arg
if(int(task_id) == 1):
    with open("../requirements.txt", 'r') as fin:
        print("Requirments.txt file content")
        print fin.read()
if(int(task_id) == 2):
    if(doc_uuid == ''):
        print(" No doc_uuid supplied")
    else:
        task_2(doc_uuid)
        print("Histograms for per country beed saved in : static/results/countries_to_book_UUID.png")
        print("Histograms for per continent beed saved in : static/results/continent_to_book_UUI.png")
elif(int(task_id) == 3):
    task_3()
    print("Histograms of browser usage has been seaved in 'static/results/simple_browser_usage.png' ")
    print("Histograms of generalised browser usage has been seaved in 'static/results/general_browser_usage.png")
elif(int(task_id) == 4):
    print("Data of 10 most active readers")
    task_4(10)
elif(int(task_id) == 5):
    if((user_uuid == '') | (doc_uuid == '')):
        print("Provide user_uuid or/and doc_uuid")
        # 938601f24509a9f1 , 110727005030-000000009cca70787e5fba1fda005c85
    else:
        task_5(user_uuid, doc_uuid)


if __name__ == "__main__":
main(sys.argv[1:])

1 个答案:

答案 0 :(得分:1)

不要从Python shell运行它,这可以设置为从终端运行为普通程序。

cd <wherever this script is>
chmod a+x ./cw.py
./cw.py -u user_745409913574d4c6 -d doc_140228202800-6ef39a241f35301a9a42cd0ed21e5fb0 -t task_2

此外,您问题中的脚本上的缩进是完全错误的,但我认为这是一个复制粘贴错误。