我如何接受用户输入但将其全部重定向到另一个文件?

How do I take in userinput but redirect all of it to another file?

我有一段代码,我想将用户键入的所有内容转移到另一个文件中,以便我可以永远保留该信息。我该怎么做?我的代码如下。请注意,我的代码完全在 python 3.6.1.

username = input('What is your name: ')

with open (username, "w") as u:
    u.write(input())

这是我当前的代码:

username = input('What is your name: ')

with open (username, "w") as u:
    u.write(input())

让我们将其更改为:

username = input('What is your name: ')
recordinput = input('This is what you want to record from the user: ')
with open (username, "w") as u:
    u.write(recordinput)
''' Now you can record 'recordinput' in the new file '''
username = input('What is your name: ')

u=open(username+".txt", "w")
while True:

    print("enter your inputs")
    inp=input()
    u.write(inp+"\n")
    #do something with inputs.
    if(inp=="exit"):
        break

u.close()

这很简单

username = input("Whats your name?")
info = input("What should I record?")

file = open(username,"w")
file.write(info)
file.close()

我更喜欢使用这种方法。 我在写入模式下打开它并写入来自第 2 行的 var 信息。