简体   繁体   English

当我尝试让 python 将用户输入写入 txt 文件时,它不会

[英]When i try to make python write user input to a txt file, it doesn't

i am learning python, and i wanted to create a little login and register program, that writes the username and the password to a txt file (so that it can be later used), i am currently working on the register function, when i try to write to the txt files it does nothing, i tried doing a with loop, a.flush() and.close() but neither of them saves the info.我正在学习 python,我想创建一个小登录和注册程序,将用户名和密码写入 txt 文件(以便以后使用),我目前正在注册 function,当我尝试要写入 txt 文件,它什么也不做,我尝试使用循环、a.flush() 和 .close(),但它们都没有保存信息。 Here's my code:这是我的代码:

        username.write = input ('username > ')
        password = open("password.txt", "w")
        password.write = input ('password > ')
        print('Welcome.')
        username.close()
        password.close() 

What am i missing?我错过了什么? Edit.编辑。 Neither 3 of the solutions to the suggested question work for me...建议问题的三个解决方案都不适合我......

Get your input and store them in two variables and then write them to files:获取您的输入并将它们存储在两个变量中,然后将它们写入文件:

username = input ('username > ')
password = input ('password > ')
with open('usernames.txt', 'w') as userFile:
   userFile.write(username)
with open('passwords.txt', 'w') as passFile:
   passFile.write(password)
yourfile.open(filename,'w')
username = input()
password = input()
yourfile.write(username)
yourfile.write(password)
yourfile.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在批处理文件中使用python脚本写入文件然后运行它时。 它不会写入文件 - When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file 我正在尝试读取日志文件。 它使用记事本作为txt文件打开。 但是,当我尝试使用python读取文件时,它无法打开 - I am trying to read a log file. It opens using notepad as txt file. However when I try using python to read the file it doesn't open 当我尝试在Django urls.py文件中编写内容时,它在浏览器中未显示 - When I try to write something in Django urls.py file, it doesn't show this in the browser 使用python make进行写入和写入文件,但不写入或关闭文件 - Make and write to file in python makes but doesn't write or close file 我无法在 file.txt 上写字 python - I can't write on file.txt python 当我尝试使它成为一个功能时不起作用 - Doesn't work when i try to make it a function 当我尝试打印登录用户的 localID 时,它不起作用 - When I try to print the localID of the logged in user it doesn't work 尝试编写要求用户输入输入文件名的python代码,它可以工作,但是当我输入输入文件时却说未定义“输入文件”? - Trying to write python code asking user for the name of the input file, it works but when I enter the input file it says 'input file' is not defined? 将用户输入写入文件 python - Write user input to file python 如何做出“无效选择”。 如果用户没有输入选项之一,请再试一次`打印? - How to make `Invalid Choice. Try again` print if the user doesn't input one of the options?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM