1

Screenshot of my IDE with the directory

I am very new to python and have already tried googling a fix for this issue, I've also tried other solutions listed on this site and nothing has worked so as a last resort I am making a post asking for help with this, I would like the code to read and write too two files "user.txt" and "passw.txt" both of which are located in the directory of the python file, but python will not see the files at all, I have tried making new files with different names but absolutly no sign of seeing the two files at all, any help is appreciated

as I said I am very new to python and the Code bellow is probably awful but I will still supply it

(the reason for the first few lines was as I originally wrote the code where No = continue, which I later changed my mind on



question1 = input(" Do You Have a User Account? Yes or No ")
if question1 == ('y') or ("Yes") or ("yes") or ("Y"):
    question1 = ('n')
elif question1 == ('n') or ("No") or ("no") or ("N"):
    question1 = ('y')
else:
    print (' That is not an option ')

user = open("user.txt")
read1 = user.read()

passw = open("passw.txt")
read2 = passw.read()


if question1 == ("y"):

    user = open("user.txt")

    read1 = user.read()



    user.close

    user = open("user.txt","a+")
    x = input("please enter a username ")
    user.write("\n")

    user.write(x)


    passw = open("passw.txt")

    read2 = passw.read()



    passw.close

    passw = open("passw.txt","a+")
    x = input("please enter a password ")
    passw.write("\n")

    passw.write(x)
else:
    print(" wait a second please")

score = (0)

user = input("please enter your username ")
user = open("user.txt","r")
read1 = user.read()
if user in read1:
    print("yes")
    score =+ (1)
    user.close()
else:
    print("no")


if score == (1):
    print("Please Continue")

any help is appriciated

already tried googling a fix for this issue, I've also tried other solutions listed on this site and nothing has worked so as a last resort I am making a post asking for help with this, I would like the code to read and write too two files "user.txt" and "passw.txt" both of which are located in the directory of the python file, but python will not see the files at all, I have tried making new files with different names but absolutely no sign of seeing the two files at all, any help is appreciated

AnAceCase
  • 11
  • 2
  • 1
    Have you tried looking at your current working directory using `import os` and `print(os.getcwd())`? You might find it is not what you expect. – B Remmelzwaal Feb 16 '23 at 23:59
  • It's running the program from another directory with an absolute path to your python executable and your script. You can `cd` to the directory that it's in or code the absolute path with something like `pathlib` – Brandon Johnson Feb 17 '23 at 00:00
  • 1
    `question1 == ('y') or ("Yes") or ("yes") or ("Y")` doesn't do what you think it does. – bn_ln Feb 17 '23 at 00:00
  • Also, your if statement booleans are wrong. Each `or` clause is a separate boolean, so the truthiness of the last three strings is checked, which is always True. – B Remmelzwaal Feb 17 '23 at 00:00
  • 1
    `open("user.txt")` This will look in _the current directory_ for that file. However, depending on how you run your code, the current directory may not be the same directory as your python script. You can use this code to show the current directory: `import os; print(os.getcwd())` – John Gordon Feb 17 '23 at 00:00
  • Welcome to Stack Overflow. "already tried googling a fix for this issue" - specifically what did you try searching for? Because when I try [copying and pasting your question title into a search engine](https://duckduckgo.com/?q=Errno+2+No+such+file+or+directory%3A+%27user.txt%27+When+there+are+files+in+the+dictionary) I get many relevant results right off the top. – Karl Knechtel Feb 17 '23 at 00:10

0 Answers0