【问题标题】:File creation with python for users and passwords使用 python 为用户和密码创建文件
【发布时间】:2020-12-08 09:54:35
【问题描述】:

我正在制作用户创建和登录系统,但是当我创建用户时,相关文件中没有任何内容。

inp = "newuser bob password"   
if inp.startswith("newuser "):
            file = open(f".{inp.split(' ')[1] }.txt", "x")
            file.close()
            file = open(f".{inp.split(' ')[1] }.txt", "w")
            file.write(str(inp.split(' ')[2]))

脚本运行没有任何问题或错误,文件被创建,但它不包含任何内容。

【问题讨论】:

  • 对我来说它把password写入.bob.txt文件
  • 写入文件后是否尝试关闭文件?
  • 一些笔记。首先,您不需要使用x 模式打开,因为w 模式无论如何都会为您创建一个新的空文件。其次,您应该在最后致电file.close()。最后,您不需要在示例中将' ' 传递给split,因为默认情况下它会在空白处拆分。
  • 强制:不要存储明文密码,更不要尝试自己实现认证,使用已建立的库。

标签: python file split


【解决方案1】:

关闭文件会写入任何更改。因此,您应该再次在末尾添加一个 close 方法。 代码:

inp = "newuser bob password"   
if inp.startswith("newuser "):
            file = open(f".{inp.split(' ')[1] }.txt", "w")
            file.write(str(inp.split(' ')[2]))
            file.close()

【讨论】:

  • 另外,不要忘记加密密码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2022-10-25
  • 2016-09-26
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
相关资源
最近更新 更多