【问题标题】:Pickle not creating and writing to a file?Pickle 没有创建和写入文件?
【发布时间】:2018-09-13 01:15:38
【问题描述】:

我正在尝试创建一个将用户输入写入文本文档的程序,但它不工作。

它只是一遍又一遍地循环这一节。这是我当前的代码:

else:
    #Creates a new file and writes the pasword to it 
    print("Strong Password")
    password1 += password1
    newName = input("What do you want to save this password as? ")
    print(newName)
    file = open("passwordstorer", "w")
    file.write(newName)
    file.write(password1)
    file.close()

    break

【问题讨论】:

  • 我们能看到更多你的代码吗?
  • 欢迎来到 Stack Overflow!其他用户将您的问题标记为低质量和需要改进。我重新措辞/格式化您的输入,使其更容易阅读/理解。请查看我的更改以确保它们反映您的意图。但我认为你的问题仍然无法回答。 现在应该edit你的问题,添加缺失的细节(见minimal reproducible example)。如果您对我有其他问题或反馈,请随时给我留言。

标签: python passwords pickle


【解决方案1】:

尝试使用with open,而不是每次都手动关闭。我怀疑它是你错误的根源,但你永远不知道。像这样: else:

print("Strong Password")
password1 += password1
newName = input("What do you want to save this password as? ")
print(newName)
with open("passwordstorer.txt, "w") as file:
    file.write(newName)
    file.write(password1)

 file.close()

`

【讨论】:

    【解决方案2】:

    我同意 v0rtex 的使用,它更干净。

    但我认为您的问题是您正在使用 input(用于整数) vs raw_input(用于原始字符串输入)

    这取决于您使用的 python 版本。 raw_input 在 python3 中不存在,输入在 python3 中用于两者。 所以如果你能验证你的 python 版本。

    另外,将原始文本密码保存在文件中可能有点危险,也许您应该考虑对其进行加盐/散列处理。

    【讨论】:

    • 是的,如果是 python2,NameError 是可能的。可能会有一个 try 静默地捕捉错误。这就是为什么我要这个版本,而不是草率下结论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2022-01-25
    相关资源
    最近更新 更多