-3

If i input data like

username = "exampleusername"
password = "examplepassword"
email = "example@example.com"
year = "2018"

software work good but i need software read data from file and if do this

file = open("data.txt", "r")
username = file.readline()
password = file.readline()
email = file.readline()
year = file.readline()

i see this statement from website where i put this information enter image description here

This problem i see if i read data from file. I not see this problem if i give data in code. But i need to software read this data from file :)

sorry for my english i stil learn him.

  • You need to show us the contents of your file and what the variables are once you have read them from the file. Please read https://stackoverflow.com/help/how-to-ask – adamfowlerphoto Jan 28 '18 at 11:16

1 Answers1

1

Assuming this is how you stored the data in you data.txt file:

exampleusername
examplepassword
example@example.com
2018

Then the problem is probably because .readline() returns a \n at the end of each line. To avoid that, you can use .strip() for example to remove that extra character like so:

file = open("data.txt", "r")
username = file.readline().strip()
password = file.readline().strip()
email = file.readline().strip()
year = file.readline().strip()

If your data.txt only has these four lines, you could do it in a shorter way like this:

username, password, email, year = open("data.txt", "r").read().splitlines()
Omar Einea
  • 2,478
  • 7
  • 23
  • 35