我试图从命令行传递一个文件以在我的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()
答案 0 :(得分:2)
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
...切换一个或另一个(参数或解析器)。