【问题标题】:Argument parser error: too few arguments参数解析器错误:参数太少
【发布时间】:2017-01-20 10:39:15
【问题描述】:

我正在尝试从命令行传递一个文件以在我的 python 代码中使用,我正在使用这样的命令:

C:\xampp\htdocs\py>twitter_checker.py -u C:\xampp\htdocs\py\test.txt

现在当我运行该命令时,我收到以下错误

用法:twitter_checker.py [-h] u

twitter_checker.py:错误:参数太少

我该如何解决这个问题,以便我可以将我的 .txt 文件传递​​给open()

# _*_ coding: utf-8 _*_

# Check if twitter usernames exist or are available

import argparse as ap
import requests

def args():
    """ Get the arguments passed from the CLINE """
    parser = ap.ArgumentParser()
    parser.add_argument("u", help="The text file with all your usernames in")
    return parser.parse_args()

def checker():
    """Loop through lines and check for available usernames"""
    argslist = args()
    usernames = open(argslist.u, "r")
    lines = usernames.readlines()
    usernames.close()

    for line in lines:
        url = "https://twitter.com/" + line
        check = requests.get(url)
        if check.status_code == 404:
            print line + ' is avaialble'
        else:
            print line + ' is taken...'
checker()

【问题讨论】:

标签: python


【解决方案1】:

parser.add_argument 不适合您作为论点的内容。

C:\xampp\htdocs\py>twitter_checker.py -u C:\xampp\htdocs\py\test.txt

应该使用:

parser.add_argument("-u", help="The text file with all your usernames in")

注意-u 而不是u...切换一个或另一个(参数或解析器)。

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 2014-08-04
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2018-04-22
    • 1970-01-01
    • 2016-08-16
    相关资源
    最近更新 更多