Question

Edit

I wrote this post when I first starting coding. My program was a "login system" and I use the term very loosely. I did not really use it much but it was a little passion program whilst I was at school. A lot has changed since then. Anyway I'll leave you to read the awful code and post. I hope it helps.


Original Post

I wrote this code to read information in from a text file, that stores all these details and it works.

loginsystem=open("loginsystem.txt","r")
myuser=loginsystem.readline()
mypass=loginsystem.readline()
print(myuser)
print(mypass)

But the text document it reads from leaves a extra line at the end of the string that is read in. Here is the desired output of the program:

myuser
mypass
user1
pass1
user2
pass2
user3
pass3
user4
pass4
user5
pass5
Was it helpful?

Solution

I think you mean that your output appears like

myuser

mypass

...

instead of

myuser
mypass

You want to strip the newline characters from your readline()s:

myuser = loginsystem.readline().rstrip()

However, it is probably better practice to use with so your file handling is properly taken care of.

with open("loginsystem.txt", "r") as loginsystem:
     ...

Additionally, if you want to read all pairs of usernames and passwords, you can do something like:

from itertools import izip_longest
with open("loginsystem.txt", "r") as loginsystem:
    for line1, line2 in izip_longest(*[loginsystem]*2):
        username = line1.rstrip()
        password = line2.rstrip()
        print username, password
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top