-2

i am learning python, and i wanted to create a little login and register program, that writes the username and the password to a txt file (so that it can be later used), i am currently working on the register function, when i try to write to the txt files it does nothing, i tried doing a with loop, a .flush() and .close() but neither of them saves the info. Here's my code:

        username.write = input ('username > ')
        password = open("password.txt", "w")
        password.write = input ('password > ')
        print('Welcome.')
        username.close()
        password.close() 

What am i missing? Edit. Neither 3 of the solutions to the suggested question work for me...

  • 1
    Possible Duplicate [Take user input and put it into a file in Python?](https://stackoverflow.com/questions/3011680/take-user-input-and-put-it-into-a-file-in-python) – GiftZwergrapper Apr 11 '20 at 14:54
  • 1
    You are assigning the write method a new value, loosing the default method code. You should be calling it with parens instead. – progmatico Apr 11 '20 at 14:54
  • @progmatico how do i do that? –  Apr 11 '20 at 14:58
  • See the example in the link of the comment above. – progmatico Apr 11 '20 at 15:04
  • Or an answer someone will post. It looks the first answer was deleted. Usually a `with` statement is used, but is not obligatory. You read your input into some object then you invoke one of the write methods of the file object passing it as an argiment. – progmatico Apr 11 '20 at 15:05

2 Answers2

1

Get your input and store them in two variables and then write them to files:

username = input ('username > ')
password = input ('password > ')
with open('usernames.txt', 'w') as userFile:
   userFile.write(username)
with open('passwords.txt', 'w') as passFile:
   passFile.write(password)
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
0
yourfile.open(filename,'w')
username = input()
password = input()
yourfile.write(username)
yourfile.write(password)
yourfile.close()
sresu
  • 1
  • 1
  • 1
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 11 '20 at 17:04