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
#2
There is a basic tutorial for files on the forum:
[Basic] Files
Reply
#3
(Sep-24-2022, 04:26 PM)Yoriz Wrote: There is a basic tutorial for files on the forum:
[Basic] Files

Thanks but I am slow learner perhaps you can help me?
Reply
#4
with open('ip_out.txt', 'w') as fp:
    for ip in results:
        print(ip)
        fp.write(ip)
Reply
#5
another suggestion is to use pandas library under python
Reply
#6
You can print into file (unpack, separate with newline and direct stream to file):

with open('ip_out.csv', 'w') as f:
    print(*results, sep='\n', file=f)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Sep-25-2022, 10:52 AM)perfringo Wrote: You can print into file (unpack, separate with newline and direct stream to file):

with open('ip_out.csv', 'w') as f:
    print(*results, sep='\n', file=f)

where do I insert this line of code?
Reply
#8
(Sep-25-2022, 06:05 PM)Calli Wrote: where do I insert this line of code?

You can use it after defining name results. Prior use will raise NameError.

If you are not interested in outputting to the screen (print) then you can replace two last lines of your code.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(Sep-25-2022, 06:05 PM)Calli Wrote: where do I insert this line of code?
You should learn to test out code when get solutions.
It's after results(you make a set) line 30 in your code.
Both mine and perfringo(a little fancier🎈) code dos the same.
# Make a test set
results = {'1.255.255.254', '2.255.255.254', '3.255.255.254'}

with open('ip_out.csv', 'w') as fp:
    print(*results, sep='\n', file=fp)

with open('ip_out1.csv', 'w') as fp:
    for ip in results:
        fp.write(f'{ip}\n')
In files,and Sets dos not have order.
Output:
2.255.255.254 3.255.255.254 1.255.255.254
Calli likes this post
Reply


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 609 3 hours ago
Last Post: Bronjer
  Start print a text after open an async task via button Nietzsche 0 699 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,110 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Saving the times a script is run to a file or ... 3Pinter 7 1,386 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 809 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,649 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  failing to print not matched lines from second file tester_V 14 6,063 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,216 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Print text with big font and style tomtom 5 14,005 Mar-03-2022, 01:29 AM
Last Post: tomtom
  Trying to determine attachment file type before saving off.. cubangt 1 2,139 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