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

前端 未结 3 814
情歌与酒
情歌与酒 2020-12-11 09:00

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 i

3条回答
  •  没有蜡笔的小新
    2020-12-11 09:42

    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.

提交回复
热议问题