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

时间:2019-06-09 10:16:02

标签: python filesystems data-storage

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

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

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

3 个答案:

答案 0 :(得分:1)

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()

答案 1 :(得分:0)

这是我当前的代码:

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 '''

答案 2 :(得分:0)

这很简单

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

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

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