当我加密了多行时,文件写入仅写入一行

2024-10-02 06:31:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试加密一个文件,并将第一个文件中的加密文本保存到另一个文件中。我有它所有的工作,除了它只是写入一行到新的文件,而不是整个文件

file1 = open("passwords-plainText", "r")
for line in file1:
    file2 = open("encryptedfile.txt", "w")
    file2.write(encryptVignere(keyGen(), line))

我使用的示例文件如下所示

example
secondexample
new line
this is another new line

我保存到的新文件中的输出只写入第一行,而不写入其余的行 (即)

tyawakud

文件应该是这样的

tyawakud
tqiibwaeeonozp
pttzucfqs
foxnzgjwtmbhnpwhjnapmsfg

Tags: 文件in文本txtnewforlineopen
1条回答
网友
1楼 · 发布于 2024-10-02 06:31:55

您应该只打开file2一次:

file1 = open("passwords-plainText", "r")
file2 = open("encryptedfile.txt", "w")

for line in file1:
    file2.write(encryptVignere(keyGen(), line))

否则,您将继续覆盖它

相关问题 更多 >

    热门问题