Python Forum
exporting all lines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: exporting all lines (/thread-32649.html)



exporting all lines - YazeedbnMohmmed - Feb-23-2021

Hello Dear

Before few days ago I was code password generator and I had problem that the output text file only write last line my code was like this

    pwd = open("passwords.txt", "w")
    pwd.write(password +"\n" )
    pwd.close()
and I changed w to a (appended) and its work and print all output lines

but today when I code md5 encryption every work ok until I want to write it to file its back to old problem only save last line


this is my code that I need to solve there are no error in command line print every thing but again only export last line

full code

import hashlib
 
with open('strings.txt') as f:
    for line in f:
        line = line.strip()
        print(f'{hashlib.md5(line.encode()).hexdigest()} ')
      
pwd = open("passwords.txt", "a")
pwd.write(str(f'{hashlib.md5(line.encode()).hexdigest()} '))
pwd.close()
       
Python 3.9
Windows 10

Best regards


RE: exporting all lines - BashBedlam - Feb-24-2021

You could write to the passwords.txt file as you read each line from the strings.txt file like this:

pwd = open("passwords.txt", "w")
with open('strings.txt', "r") as f:
    for line in f:
        line = line.strip()
        pwd.write(str(f'{hashlib.md5(line.encode()).hexdigest()} '))
pwd.close()



RE: exporting all lines - YazeedbnMohmmed - Feb-24-2021

Work like a charm

Thx mate