【发布时间】: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