我试图从命令行传递一个文件以在我的 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/