Reputation: 288
I want to create a Programm that saves login information in a file. So I think the best way is to write the username and password like this:
username # password
But how to writhe it so and how to read it again? My Code writes in 3 lines instead of one:
file = open(“testfile.txt”,”w”)
file.write(username)
file.write(“#”)
file.write(password)
file.close()
file = open(“testfile.txt”, “r”)
print file.read(1)
How can I make the programm only write in one line?
Edit I need to save to 2 Values in the variables again.
Upvotes: 0
Views: 95
Reputation: 1442
This will let you write both usernames and passwords in one line:
username, password = 'username', 'password'
with open("testfile.txt", 'w') as file:
file.write(f"{username} # {password}")
To read from your file
lines = []
with open('testfile.txt', 'r') as file:
for line in file:
lines.append(line)
print(lines[0]) # this will print line 1
Upvotes: 1
Reputation: 461
For reading you can use the split function:
print(file.readline().split('#'))
Upvotes: 0