Take user input and put it into a file in Python?

I must have skipped a page or two by accident during my PDF Tutorials on Python commands and arguments, because I somehow cannot find a way to take user input and shove it into a file. Don't tell me to try and find solutions online, because I did. None made sense to me. EDIT: I am using Python 3.1.2, sorry for forgetting

asked Jun 10, 2010 at 4:42 456 2 2 gold badges 7 7 silver badges 12 12 bronze badges

4 Answers 4

Solution for Python 3.1 and up:

filename = input("filename: ") with open(filename, "w") as f: f.write(input()) 

This asks the user for a filename and opens it for writing. Then everything until the next return is written into that file. The "with. as" statement closes the file automatically.

19.2k 16 16 gold badges 52 52 silver badges 65 65 bronze badges answered Jun 10, 2010 at 5:33 166 3 3 bronze badges

Thanks a bunch, you really helped me. I was making a command-line and i wanted users to change their username and password from the regular "root" and "alpine".

Commented Jun 10, 2010 at 5:36 Just in case you don't already know it: docs.python.org/py3k/library/getpass.html Commented Jun 10, 2010 at 5:50

well, I want to stick to my plan, I already spent quite a lot of hours making it, it will be up on my website soon for developers. Thanks for the link though!