Python Forum
Saving the print result in a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving the print result in a text file
#1
Hi guys another day another issue.. So I have this code

from contextlib import suppress
from ipaddress import IPv6Address, ip_address
 
 
def make_ipv4_set(file_like):
    results = set()
 
    for line in map(str.strip, file_like):
 
        addr = None
        with suppress(ValueError):
            addr = ip_address(line)
 
        if isinstance(addr, IPv6Address):
            continue
 
        results.add(line)
 
    return results
 
 
with (
    open("ip1_file.txt") as ip1_file,
    open("ip2_file.txt") as ip2_file,
):
    ip1_set = make_ipv4_set(ip1_file)
    ip2_set = make_ipv4_set(ip2_file)
 
 
results = ip1_set - ip2_set
 
for ip in results:
    print(ip)
Which does the work like i expect it to but what I need is instead of it printing the output in a terminal I want to output the result in a text file. So can anyone help me with the code. Much appreciated thank you
Reply


Messages In This Thread
Saving the print result in a text file - by Calli - Sep-24-2022, 04:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,021 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  Start print a text after open an async task via button Nietzsche 0 729 May-15-2023, 06:52 AM
Last Post: Nietzsche
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,153 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Saving the times a script is run to a file or ... 3Pinter 7 1,435 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 831 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,726 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  failing to print not matched lines from second file tester_V 14 6,172 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,238 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Print text with big font and style tomtom 5 14,218 Mar-03-2022, 01:29 AM
Last Post: tomtom
  Trying to determine attachment file type before saving off.. cubangt 1 2,185 Feb-23-2022, 07:45 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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