首页 > 解决方案 > 没有使用 Python 的此类文件或目录

问题描述

我正在尝试用python编写一个程序,它从文件中读取用户名列表并向instagram发送请求以查看用户名是否可用。然后它应该提示一条消息说“此用户名可用”或“此用户名已被使用”。我在尝试读取我的文件时遇到错误,该文件指出“FileNotFoundError: [Errno 2] No such file or directory:'list.txt”,即使该文件位于程序所在的文件夹中。

我对 python 也很陌生。

这是我目前的代码:

import requests
filepath = 'list.txt'
separator = "\n" #Every time you use a newline it detects it as a new user
list  = open(filepath, "r").read().split(separator)
notTaken = []
for i in list :
    response = requests.get("https://instagram.com/" + i + "/")
    if response.status_code == 404 :
        notTaken.append(i)

标签: pythonfile

解决方案


它在我的情况下有效。我list.txt与我的 python 文件在同一个目录中

import requests
with open('list.txt','r') as f:
    data = f.read().split("\n")

for i in data:
    pass
    #...(write the rest of the code)

推荐阅读