python,获取用户输入,并且将输入的内容写到.txt

该功能缺点是必须保证该文件不存在的情况才会成功

f=open('E:/mywork/保存文件.txt','x')

def userwrite(code):
    if code=='w':
        f.close()
        return False
    else:
        f.writelines(code+'
')
        return True
isbool = True
count = 0
while isbool:
    code=''
    if count==0:
        code = input("请输入内容:
")
    else:
        code = input("")
    isbool = userwrite(code)
    count+=1
原文地址:https://www.cnblogs.com/pengpengzhang/p/8675722.html