简体   繁体   English

参数解析器错误:参数太少

[英]Argument parser error: too few arguments

I am trying to pass over a file from the command line to use in my python code, I am using the command like so: 我正在尝试从命令行传递一个文件以在我的python代码中使用,我正在使用如下命令:

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

Now when I run that command I get the following error 现在,当我运行该命令时,出现以下错误

usage: twitter_checker.py [-h] u 用法:twitter_checker.py [-h] u

twitter_checker.py: error: too few arguments twitter_checker.py:错误:参数太少

How can I fix this so I can pass my .txt file over to use in the open() 如何解决此问题,以便可以将我的.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 is incorrect for what you are putting as the argument. parser.add_argument对于您作为参数放置的内容不正确。

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

Should be using: 应该使用:

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

Notice -u instead of u ... switch one or the other (either the argument, or the parser). 注意-u而不是u ...切换一个或另一个(参数或解析器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM