I'm trying to locate two different files both stored in the same directory, but I am forced to use different syntax for each file.
I'm setting up a login interface that uses a file "usernames.txt" to store usernames and another file "passwords.txt" to store passwords. When executing seemingly the same line of code for these sister files, it requires a different file location syntax for each file. "login/usernames.txt" and "login\passwords.txt". I tried "login/passwords.txt" and got no errors but when trying "login\usernames.txt", I immediately got an error. (the difference is the slant of the slash: / or \)
This runs without an issue:
def __init__(self):
with open("login/usernames.txt", 'r') as username_file:
for username in username_file:
self.usernames_db.append(username[:-1])
with open("login\passwords.txt", 'r') as password_file:
for password in password_file:
self.passwords_db.append(password[:-1])
This gives me an error (difference is from / to \ in "login\usernames.txt"):
def __init__(self):
with open("login\usernames.txt", 'r') as username_file:
for username in username_file:
self.usernames_db.append(username[:-1])
with open("login\passwords.txt", 'r') as password_file:
for password in password_file:
self.passwords_db.append(password[:-1])
The error I receive when "login\usernames.txt" is used to locate the file:
Traceback (most recent call last):
File "C:/Users/zackj/PycharmProjects/NewProject/main.py", line 2, in <module>
from login.login import Login
File "C:\Users\zackj\PycharmProjects\NewProject\login\login.py", line 11
with open("login\usernames.txt", 'r') as username_file:
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 5-6: truncated \uXXXX escape
I understand it runs and I might just want to let it be, being how inexperienced I am with python. But I want to know why it runs with one file and not the other. I'd rather understand my code than just blindly code.