Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exporting all lines
#1
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
Reply
#2
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()
Reply
#3
Work like a charm

Thx mate
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 4,638 Aug-01-2022, 06:08 PM
Last Post: paulyan
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,212 Oct-27-2021, 10:42 AM
Last Post: Rafa
  Exporting a huge dataFrame stylingpat 5 15,271 Mar-23-2021, 12:13 AM
Last Post: stylingpat
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,707 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Python code for exporting table using Selenium gj31980 4 2,934 Aug-04-2020, 01:29 AM
Last Post: gj31980
  Exporting data from python into excel Zankawah 5 3,309 Jun-02-2020, 03:17 AM
Last Post: buran
  Exporting list with dictionary to Excel veromi22 0 3,012 Oct-15-2019, 12:54 AM
Last Post: veromi22
  Exporting to Excel Nirmal 5 2,690 Aug-08-2019, 12:22 PM
Last Post: Nirmal
  Exporting Python Output to Notepad Nirmal 4 3,521 Sep-02-2018, 02:36 PM
Last Post: Nirmal
  Exporting Json events to a text file uklipse 3 2,618 Aug-21-2018, 02:12 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020