【问题标题】:Not getting output to file没有输出到文件
【发布时间】:2019-05-19 09:36:58
【问题描述】:

我正在尝试制作一个接受输入并将其保存到文件的程序。之后,我希望使用 6 个班次的凯撒班次对文档中的信息进行加密。然后我想要一个解密程序,在其中我将文件解密回原始文件,(通过使用大致相同的代码但改为解密来完成?)

这是我的代码:

encrypt = str.maketrans('abcdefghijklmnopqrstuvwxyz0123456789', 'ghijklmnopqrstuvwxyz0123456789abcdef')
decrypt = str.maketrans('ghijklmnopqrstuvwxyz0123456789abcdef', 'abcdefghijklmnopqrstuvwxyz0123456789')

filename = "abc.abd.txt"
with open(filename, "a+") as r:
    with open(filename+'-encrypted.txt', 'w+'):
        for line in r:
            print(line.translate(encrypt), file=r)

另外,我想在它被解密成另一个文件后删除带有明文信息的文件,如果我没记错的话,这样做是这样的:

open(filename, "w+") # At the end of the document.

我的问题是运行程序后,我的新加密文档中没有输出。另外,有没有在这个程序中包含大写字母的好方法?

提前感谢所有帮助!

【问题讨论】:

  • 您在寻找正确的输出文件名吗?它将是abc.abd.txt-encrypted.txt

标签: python python-3.x encryption caesar-cipher


【解决方案1】:

您似乎没有在“file-encrypted.txt”中写入任何内容

改变

with open(filename+'-encrypted.txt', 'w+'):

with open(filename+'-encrypted.txt', 'w+') as fEncrypted:

还有

fEncrypted.write(line.translate(encrypt))

而不是

print(line.translate(encrypt), file='r’) 

关于在该过程之后删除第一个“文件名”,如果您将该文件打开为“w”,它将完成,因为您什么都没有覆盖它。要完全删除您需要做的文件:

import os
if os.path.exists("filename.txt"):  
    os.remove("filename.txt")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多