Python: Open And Write To A .txt File
I'm new to python and so I'm trying to make an ATM (simulator I guess). One of the functions is to register, and so I'm trying to save the usernames in a .txt file for reference (p
Solution 1:
I think it should be:
open("users.txt","w")
Solution 2:
You should use
withopen("users.txt","a") as f:
f.write(username)
instead of
withopen("users.txt") as f:
f.write(username, "a")
hope this helps!
Solution 3:
Given that you're calling f.write(username, "a")
and file write()
only takes one parameter - the text to write - I guess you intend the "a"
to append to the file?
That goes in the open()
call; other answers telling you to use open(name, "w")
are bad advice, as they will overwrite the existing file. And you might want to write a newline after the username, too, to keep the file tidy. e.g.
withopen("users.txt", "a") as f:
f.write(username + "\n")
Solution 4:
You must open the file in write mode. See the documentation for open
open('users.txt', 'w')
should work.
Post a Comment for "Python: Open And Write To A .txt File"