-3
with open('Passwords.txt') as passwords:
    for line in passwords:
        print(line)

I just typed this code and it gives me

FileNotFoundError: [Errno 2] No such file or directory: 'Passwords.txt'   

and I'm sure that this file in the same folder that I've put python file in. That's Proof: Files in the same folder

Youssef
  • 1
  • 1
  • 4
    From where are you running your script ? If you are running you script like this: ‘python mypath/myscript.py this will never work. You need to run you script from the root folder – CyDevos Jul 08 '21 at 17:01
  • Does this answer your question? [Why am I getting a FileNotFoundError?](https://stackoverflow.com/questions/17658856/why-am-i-getting-a-filenotfounderror) – wjandrea Jul 08 '21 at 17:01
  • FileNotFoundError is raised when you try to open a file but that file is not found. can you check if the file is in the same place that contains your code? – Kromydas Jul 08 '21 at 17:26

1 Answers1

-2

You should first enter the full path, here is an example:

with open(r'/home/user/Documents/Passwords.txt') as passwords:
    # and don't forget this
    content = passwords.readlines()
    for line in passwords:
        print(line)
Developeeer
  • 110
  • 1
  • 1
  • 10
  • That has nothing to do with ```FileNotFoundError``` –  Jul 08 '21 at 17:02
  • Nah it just defines each line to this variable (Content) as a list The program can not read the file – Youssef Jul 08 '21 at 17:05
  • yeah it did but just I want to make my program read the file without giving the absolute path. that's because If i give this program to someone he hasn't to put the program in a specific place does that make sense? – Youssef Jul 08 '21 at 17:15
  • 1
    @Youssef, sorry but you didn't mention that in your question – Developeeer Jul 08 '21 at 17:25