1

I am trying to write a program in python where it reads a list of usernames from a file and sends a request to instagram to see if the username is available or not. Then it should prompt a message saying "THIS USERNAME IS AVAILABLE" or otherwise "THIS USERNAME IS TAKEN". I am having an error when trying to read my file that states 'FileNotFoundError: [Errno 2] No such file or directory: 'list.txt', even though the file is in my folder where the program resides.

I am also fairly new to python.

Here is my code currently:

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)
  • 4
    Depending on how you run the program, the directory where the .py file resides is not the same directory as the working directory of the program (which is what relative file paths refer to). – mkrieger1 Oct 04 '20 at 18:45
  • 2
    I agree - Are you running the program from the same directory in which the file exists or are you running from a directory outside? – Nikhil Baliga Oct 04 '20 at 18:46
  • 1
    Same directory in which the file exists @NikhilBaliga –  Oct 04 '20 at 18:47
  • 1
    Does this answer your question? [Why am I getting a FileNotFoundError?](https://stackoverflow.com/questions/17658856/why-am-i-getting-a-filenotfounderror) – mkrieger1 Oct 04 '20 at 18:48
  • 1
    Can you try using `filepath = r ".\list.txt"` and report the output? And are you sure that its a `.txt` file ? . –  Oct 04 '20 at 18:49
  • Keep getting either invalid syntax or the same error and yes my file is a text document @NeoNØVÅ7 –  Oct 04 '20 at 18:58
  • change `filepath` to the complete path to the file including the drive. Did it work? – wwii Oct 04 '20 at 18:59
  • 1
    @PythonBoi , Maybe you should consider closing the file if you haven't using `list.close()` –  Oct 04 '20 at 19:00
  • import the os module and just before opening the file print the current directory `print(os.getcwd())` - was it what you expected? – wwii Oct 04 '20 at 19:01
  • tried filepath = "\Users\disre\Desktop\Python Testing\list.txt" @wwii same thing –  Oct 04 '20 at 19:02

3 Answers3

0

It worked in my case. My list.txt was in the same directory as my python file

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)
  • 1
    Conversely , if you are not sure if `list.txt` exists in your Working Directory , you can just make the second line as `with open('list.txt','r+') as f:` . The `+` after `"r"` will create a new file for you if it doesn't exists. –  Oct 04 '20 at 19:10
0

Use the absolute directory for that file i.e /home/directory/file_name if you are using window else use c:\\\\directory\\file_name this is if the file is not in the current working directory of the terminal. If you are in the same directory of the file in the terminal, use./file_name

Ekure Edem
  • 310
  • 2
  • 10
0

Since the file is in the same directory as your program, your code will only work if the current working directory is also that same directory. If you want to run this program from anywhere, you need a way to find that directory.

When python executes a script or module it adds a __file__ variable giving the path. (Extension modules and modules in compressed packages may not have this variable). The __file__ path can be relative to the current working directory or absolute. The pathlib library gives us a handy method for working with paths. So,

from pathlib import Path
separator = "\n" #Every time you use a newline it detects it as a new user
filename = 'list.txt'
# get absolute path, then parent directory, then add my file name
filepath = Path(__file__).resolve().parent/filename
mylist = filepath.open().read().split(separator)

Since a "\n" newline is used to demark the file, you don't need to split it yourself. You could do this instead

mylist = filepath.open().readlines()
tdelaney
  • 73,364
  • 6
  • 83
  • 116