简体   繁体   English

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

[英]File write is only writing one line when I have encrypted multiple lines

I am trying to encrypt 1 file and save the encrypted text from the first file into another file.我正在尝试加密 1 个文件并将加密文本从第一个文件保存到另一个文件中。 I have it all working besides that it is only writing 1 line into the new file instead of the entire file.除了它只将 1 行写入新文件而不是整个文件之外,我已经完成了所有工作。

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

The example file I am using looks like this我正在使用的示例文件如下所示

example
secondexample
new line
this is another new line

The output into the new file I am saving to only writes the first line and not the rest of the lines ie.)输出到我保存的新文件中只写入第一行而不是其余行,即。)

tyawakud

The file should look like this instead...该文件应该看起来像这样......

tyawakud
tqiibwaeeonozp
pttzucfqs
foxnzgjwtmbhnpwhjnapmsfg

You should only open file2 once:你应该只打开file2一次:

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

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

Otherwise you just keep overwriting it.否则你只会继续覆盖它。

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

相关问题 循环打印多行(按预期),但只向文件写入一行 - Loop prints multiple lines (as intended) but only writes one line to the file file.write(line)不写文件中的所有行 - file.write(line) not writing all of the lines in the file 如果我有一个字典,其中行的键和内容是文本,如何轻松地将行写入文本文件? - How do I write lines to a text file easily if I have a dictionary with the key of the line and contents being the text? Python:尝试将字符串写入新文件时,仅写入字符串的最后一行 - Python: Writing only the last line of the string when trying to write the string to a new file 只将所有分割行的一个字符串写入文件 - writing only the one string of all splitted lines to a file 一行写jinja2宏和多行写宏的区别? - Difference of writing jinja2 macro in one line V.S. writing macro in multiple lines? 在Python中写入文件时如何写入新行? - How do you write to a new line when writing to a file in Python? Python:编写解析器以一次读取多行时的文件结尾 - Python: End of File when writing a parser that reads multiple lines at a go 当我只要求输入1时,为什么在文本文件中写入2行? - Why does it write to 2 lines in a text file when I only ask it to type 1? Python中的问题,函数仅向文件写入一行 - Problem in Python with function only writing one line to file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM