Why the context of the file cannot read from the txt file? - python

I have creat a new empty txt file, but the code below read and write it.
f = open('users.txt', 'r+')
users = eval(f.read()) #f.read()read a string,eval()transfer string to dict
for i in range(4):
name = input('Input Username: ')
passwd = input('Input password: ')
c_passwd = input('Confirm password again: ')
if len(name.strip()) != 0 and name not in users and len(passwd.strip()) != 0 and passwd == c_passwd:
users[name]= {'passwd':passwd, 'role':1} #insert new data, role 1: Customer; role 2: Restaurant; role 3: Admin
f.seek(0)
f.truncate() #clear file
f.writelines(str(users)) #write data to file from dict
print('Congratulations, Register Success. ')
f.close()
break
elif len(name.strip()) == 0:
print('Username could not be empty. Remain %d chance' %(3-i))
elif name in users:
print('Username repeat. Remain %d chance' %(3-i))
elif len(passwd.strip()) == 0:
print('Password could not be empty. Remain %d chance' %(3-i))
elif c_passwd != passwd:
print('Password not same. Remain %d chance' %(3-i))
#log in
f = open('users.txt', 'r', encoding='utf8')
users = eval(f.read())
for count in range(3):
name = input('Input Username: ')
password = input('Input password: ')
if name in users and password == users[name]['passwd']:
print('Log in successful!')
break
else:
print('Username or/and Password is/are wrong,You still have %d chance'%(2-count))
f.close()
The System showed
Traceback (most recent call last):
File "C:/Users/zskjames/PycharmProjects/Fit5136/Register, log in.py", line 4, in <module>
users = eval(f.read()) #f.read()read a string,eval()transfer string to dict
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
Do anybody could tell me how to fix that? And how to avoid this mistakes in the future.

You probably want your text file to contain JSON, in order to easily interact with it and turn it into a dict.
In order to do that, you would need to replace your eval with a json.load:
import json
with open('users.txt', 'r+') as f:
users = json.load(f)
# rest of your code
In order for it to work, your text file should look something like the following:
{"John Doe": {"passwd": "somepass", "role": 1}}
In addition, you need to replace:
f.writelines(str(users)) #write data to file from dict
to:
json.dump(users, f)

Related

Python validate input against text file

file=open('New Text Document.txt','w')
lines=file.writelines(['username:ds_jr\n','password:89120&%'])
file.close()
file=open('New Text Document.txt','r')
file.readlines()
username=input('username:')
password=input('password:')
check=['username:'+username+'\n','password:'+password]
if check == lines:
print('hello ds_jr , welcome!')
else:
print('not found')
How to compare input username and password against .txt file content.
file=open('New Text Document.txt','w')
lines=file.writelines(['username:ds_jr\n','password:89120&%'])
file.close()
username=input('username:')
password=input('password:')
file = open('New Text Document.txt')
check = file.read()
if username and password in check:
print('hello ds_jr , welcome!')
else:
print('not found')
Lookups in Python are best implemented in dictionaries. Dictionaries can also be useful for holding the information that you want to write into the file. In this case there are only two values but for flexibility let's put those into a dictionary to begin with.
Choose a format for the file that can be understood later when reading.
When the file is read, build a new dictionary to store the keywords and values.
Now you can get the user input and check the values entered against the new dictionary.
FILE = 'New Text Document.txt'
USERNAME = 'username'
PASSWORD = 'password'
out_d = {USERNAME: 'ds_jr', PASSWORD: '89120&%'}
with open(FILE, 'w') as f:
for k, v in out_d.items():
print(f'{k}={v}', file=f)
in_d = dict()
with open(FILE) as f:
for line in map(str.strip, f):
if (eq := line.find('=')) >= 0:
in_d[line[:eq]] = line[eq+1:]
username = input(f'{USERNAME}: ')
password = input(f'{PASSWORD}: ')
if username == in_d.get(USERNAME) and password == in_d.get(PASSWORD):
print("You're good to go")
else:
print('Incorrect username or password')
Now let's say that you want to keep more information in the file. All you have to do is edit the in_d dictionary appropriately with no need to make any other modifications to the code

How do I select certain lines in a text file from python script?

So I'm making a python script where you can create an account and that account is saved in a text file. When you try to log in, it will look in the text file for your username and then move down a line for the password but I don't know how to move down a line after finding the username. Any help would be appreciated. :)
Update -
import time
import sys
print ("Do you have an account?")
account = input()
if account == "Yes":
print ("Enter your username")
enterUsername = input()
with open ("Allusers.txt") as f:
if enterUsername in f.read():
print ("Enter your password")
enterpassword = input()
if enterpassword in f.read():
print ("Logged in")
if enterpassword not in f.read():
print ("Wrong password")
if account == "No":
print ("Create a username")
createUsername = input()
with open ("Allusers.txt") as f:
if createUsername in f.read():
print ("Username already taken")
sys.exit()
if createUsername not in f.read():
print ("Create a password")
createPassword = input()
with open ("Allusers.txt") as f:
if createPassword in f.read():
print ("Password not available")
sys.exit()
if createPassword not in f.read():
file_object = open ('Allusers.txt', 'a')
file_object.write("" + createUsername + "\n")
file_object.close()
file_object = open ('Allusers.txt', 'a')
file_object.write("" + createPassword + "\n")
file_object.close()
print ("Done")
This is still work in progress and most likely still has errors here and there.
Assumin that your file look like this:
Adam
password
John
12345678
Horacy
abcdefg
Romek
pass1234
You can try this example:
user = "Horacy"
password = "abcdefg"
with open( "users.txt", "r" ) as file:
for line in file:
if user == line.strip():
if password == file.readline().strip():
print( "Correct" )
break
As stated if someones password equals someones username iterating over all lines and checking may return faulty results you'll want to check only usernames as you iterate, so zipping every other line you can check the username only and return the password:
def get_password(file, username):
with open(file, "r") as f:
data = f.readlines()
for user, pw in zip(data[::2], data[1::2]):
if user.strip() == username:
return pw.strip()
def get_password(file, username):
lines = open(file, "r").readlines() # get the lines from the file
for i, line in enumerate(lines):
if line == username: # if the current is the username, return the following line
return lines[i + 1]
You should only search in usernames. The data[::2] will select usernames.
with open("filename", "r") as f:
data = f.read().splitlines()
email = "email#email"
if email in data[::2]:
id_email=data[::2].index(email)
row=id_email*2-1
password=data[row+1]

Python beginner - using constant to open file traceback

I'm trying to start using constants in my project and this happened.
Traceback (most recent call last):
File "C:\Users\aletr\Desktop\Python projects\Restaurant software\r_0.py", line 39, in <module>
with constants.NAMES_R as f :
File "C:\Python30\lib\io.py", line 456, in __enter__
self._checkClosed()
File "C:\Python30\lib\io.py", line 450, in _checkClosed
if msg is None else msg)
ValueError: I/O operation on closed file.
I know is because the file it's been closed. But I don't understand how I was using the same code without constant and it would work perfectly.
constants:
F_NAMES = 'names.txt'
NAMES_R = open(F_NAMES, 'r+')
NAMES_W = open(F_NAMES, 'w+')
script:
import constants
with constants.NAMES_R as f :
f_n = f.read().splitlines()
print("Welcome to NAME.app")
##############
# USER LOGIN #
##############
while True:
name = input("""
\n - Insert name to logg in
\n - ADD to save new user
\n - LIST to see saved users
\n - REMOVE to delete a user
\n - EXIT to finish
\n - ...""")
lname = name.lower()
if lname == "add":
n_input = input("Name:")
with open('names.txt', 'a') as f:
f.write(n_input + '\n')
elif lname == "list":
with constants.NAMES_R as f :
print(f.read().splitlines())
f.close()
elif name in f_n:
print("Logged as", name.upper())
user = name
input('Welcome, press enter to continue \n')
break
elif lname == 'remove':
remove = input("Insert user name to remove \n ...")
with constants.NAMES_R as f :
lines = f.readlines()
lines = [line for line in lines if remove not in line]
with constants.NAMES_W as f :
f.writelines(lines)
elif lname == "exit":
exit()

how to fix python login script using txt files to login and register users

I wrote a python script for logging people in and registering. it used a txt file to store the usernames and passwords. I wrote it in http://trinket.io. However, It does not work in regular python. can anyone tell me what i need to change to fix it?
edit:
here is the code
file = open('accounts.txt', 'a+')
lines = file.readlines()
login = {}
for line in lines:
key, value = line.strip().split(', ')
login[key] = value
while True:
command = input('$ ')
command_list = command.split(' ')
if command_list[0] == 'login':
username = command_list[1]
password = command_list[2]
try:
if login[username] == password:
print('login')
else:
print('no login')
except KeyError:
print('no login')
elif command_list[0] == "register":
file.write("\n")
file.write(command_list[1])
file.write(", ")
file.write(command_list[2])
elif command_list[0] == "help":
print("""To login, type login, then type the username and then type the password.
To register, type register, then type the username and then the password.""")
elif command_list[0]== "quit":
break
else:
print('unrecognised command')
The following edits, marked by ##### ADDED LINE should solve your issue.
Explanations:
(1) You need to use .seek() before you read from a file that was opened in a+ mode.
(2) Using .flush() will force whatever data is in the buffer to be written to file immediately.
(3) Without me restructuring your program too much, this edit allows you to immediately access the newly registered user to login with. This is because, as the program is structured now, you only add details to your login dictionary when you first open the accounts file.
file = open('stack.txt', 'a+')
file.seek(1) ##### ADDED LINE (1)
lines = file.readlines()
login = {}
for line in lines:
key, value = line.strip().split(', ')
login[key] = value
...
elif command_list[0] == "register":
file.write("\n")
file.write(command_list[1])
file.write(", ")
file.write(command_list[2])
file.flush() ##### ADDED LINE (2)
login[command_list[1]] = command_list[2] ##### ADDED LINE (3)
Hope this helps!

Change text in file with Python

def false_to_true():
name = input("Input name: ")
file=open("users.txt","r")
lines = file.readlines()
file.close()
for line in lines:
username, lel, type = line.split("/")
while name == username:
name = input("input name again: ")
tip = True
with open("users.txt", "w") as users:
users.write(str(red))
#
#I do not know how to perform a given modification and enrollment into place in #the text.
#
#I wont to change word False to True for username i input.
#I have this text in file users:
#Marko123/male/False
#Mimi007/female/False
#John33/male/False
#Lisa12/female/False
#Inna23/female/False
#Alisa27/female/False
I won't to change word False to True for username I input.
I have this text in file users:
Marko123/male/False
Mimi007/female/False
John33/male/False
Lisa12/female/False
Inna23/female/False
Alisa27/female/False
You can just use the csv library and forget about string manipulation:
import csv
def false_to_true():
#read from user.txt file into list(data)
with open('users.txt', 'r') as userfile:
data = [row for row in csv.reader(userfile,
delimiter="/",
quoting=csv.QUOTE_NONE)]
while True:
#waiting for input until you enter nothing and hit return
username = input("input name: ")
if len(username) == 0:
break
#look for match in the data list
for row in data:
if username in row:
#change false to true
row[2] = True
#assuming each username is uniqe break out this for loop
break
#write all the changes back to user.txt
with open('users.txt', 'w', newline='\n') as userfile:
dataWriter = csv.writer(userfile,
delimiter="/",
quoting=csv.QUOTE_NONE)
for row in data:
dataWriter.writerow(row)
if __name__ == '__main__':
false_to_true()
Open the input and output files, make a set out of the user-input names (terminated by a blank line), then create a generator for strings of the proper format that check for membership in the user-input names, then write these lines to the output file:
with open('names.txt') as f, open('result.txt', 'w') as out:
names = {name for name in iter(input, '')}
f = ('{}/{}/{}'.format(a,b,'True\n' if a in names else c) for a,b,c in (line.split('/') for line in f))
output.writelines(f)
To modify a text file inplace, you could use fileinput module:
#!/usr/bin/env python3
import fileinput
username = input('Enter username: ').strip()
with fileinput.FileInput("users.txt", inplace=True, backup='.bak') as file:
for line in file:
if line.startswith(username + "/"):
line = line.replace("/False", "/True")
print(line, end='')
See How to search and replace text in a file using Python?
Ask for name and iterate throw your lines to check for username, like this:
def false_to_true():
name = input("Input name: ")
file=open("users.txt","r")
lines = file.readlines()
file.close()
users = open("users.txt", "w")
for line in lines:
username, lel, type = line.split("/")
if name == username:
type = 'True\n'# \n for new line type ends with '\n'
users.write("/".join([username, lel, type]))
users.close()
false_to_true()

Categories

Resources