python - 参数解析器错误 : too few arguments

标签 python

我试图从命令行传递一个文件以在我的 python 代码中使用,我使用的命令如下:

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

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

usage: twitter_checker.py [-h] u

twitter_checker.py: error: too few arguments

如何解决这个问题,以便我可以传递我的 .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()

最佳答案

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...切换其中之一(参数或解析器)。

关于python - 参数解析器错误 : too few arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41755152/

相关文章:

python - 如何在 Python 中使用 librt 函数?

python - 访问https网页时获取urllib2权限被拒绝错误?

python - 如何从日期中减去一天?

python - 在 __init__() 中不创建对象的最佳方法是什么

python - 如何在带有tensorflow v2.x后端的keras中加载带有tensorflow v1.x后端的keras模型?

python - 经过训练的机器学习模型太大

python - Pyspark - 将另一列添加到稀疏向量列

python - 获取嵌套字典中的公共(public)值

python - 为什么 _ in range(n) 比 _ in [""]*n 慢?

python - Pandas unstack 问题 : ValueError: Index contains duplicate entries, 无法 reshape