Search code examples
pythonstringnotepad

exporting python string output to notepad


I am trying to export a string output to notepad on lines 19 and 20.

Code section, lines 1-21

1    import ipaddress
2    import socket
3    import sys
4    import os
5    
6    #networx prefix calc
7    def f1():
8        try:
9            network = input('please input network address with the prefix: ')
10           network = ipaddress.ip_network(network)
11           print(network)
12   
13       except ValueError:
14           print('That is not a network address')
15   
16       iplist= list(ipaddress.ip_network(network).hosts())
17       for i in range(10,len(iplist),2):
18           print(iplist[i])
19   
20       notepad1 = open('oddnetworks.txt','w')
21       notepad1.write(iplist)

This is my output, which is a range of odd numbered IP addresses. Please scroll down to the bottom of the output to view the error.

The error, that I am receiving is, TypeError: write() argument must be str, not list I dont know how, to convert line 21 to a string.

Output

Please enter 'network' to calculate odd number IP address ranges
Please enter 'port' to scan ports 19-81
to exit this program, enter 'exit'
note; due to performance issues, you may have to input your choice twice
please input your choice:  network
please input your choice:  network
please input network address with the prefix: 182.168.2.0/24
182.168.2.0/24
182.168.2.11
182.168.2.13
182.168.2.15
182.168.2.17
182.168.2.19
182.168.2.21
182.168.2.23
182.168.2.25
182.168.2.27
182.168.2.29
182.168.2.31
182.168.2.33
182.168.2.35
182.168.2.37
182.168.2.39
182.168.2.41
182.168.2.43
182.168.2.45
182.168.2.47
182.168.2.49
182.168.2.51
182.168.2.53
182.168.2.55
182.168.2.57
182.168.2.59
182.168.2.61
182.168.2.63
182.168.2.65
182.168.2.67
182.168.2.69
182.168.2.71
182.168.2.73
182.168.2.75
182.168.2.77
182.168.2.79
182.168.2.81
182.168.2.83
182.168.2.85
182.168.2.87
182.168.2.89
182.168.2.91
182.168.2.93
182.168.2.95
182.168.2.97
182.168.2.99
182.168.2.101
182.168.2.103
182.168.2.105
182.168.2.107
182.168.2.109
182.168.2.111
182.168.2.113
182.168.2.115
182.168.2.117
182.168.2.119
182.168.2.121
182.168.2.123
182.168.2.125
182.168.2.127
182.168.2.129
182.168.2.131
182.168.2.133
182.168.2.135
182.168.2.137
182.168.2.139
182.168.2.141
182.168.2.143
182.168.2.145
182.168.2.147
182.168.2.149
182.168.2.151
182.168.2.153
182.168.2.155
182.168.2.157
182.168.2.159
182.168.2.161
182.168.2.163
182.168.2.165
182.168.2.167
182.168.2.169
182.168.2.171
182.168.2.173
182.168.2.175
182.168.2.177
182.168.2.179
182.168.2.181
182.168.2.183
182.168.2.185
182.168.2.187
182.168.2.189
182.168.2.191
182.168.2.193
182.168.2.195
182.168.2.197
182.168.2.199
182.168.2.201
182.168.2.203
182.168.2.205
182.168.2.207
182.168.2.209
182.168.2.211
182.168.2.213
182.168.2.215
182.168.2.217
182.168.2.219
182.168.2.221
182.168.2.223
182.168.2.225
182.168.2.227
182.168.2.229
182.168.2.231
182.168.2.233
182.168.2.235
182.168.2.237
182.168.2.239
182.168.2.241
182.168.2.243
182.168.2.245
182.168.2.247
182.168.2.249
182.168.2.251
182.168.2.253
Traceback (most recent call last):
  File "C:\Users\shane\PycharmProjects\RodCertIV\ITCNWK409.py", line 58, in <module>
    f1()
  File "C:\Users\shane\PycharmProjects\RodCertIV\ITCNWK409.py", line 21, in f1
    notepad1.write(iplist)
TypeError: write() argument must be str, not list

I want the program, to save the output of odd numbered host IP addresses to a notepad file.

Any assistance will be appreciated :)

FULL CODE FOR TESTING

import ipaddress
import socket
import sys
import os

#networx prefix calc
def f1():
    try:
        network = input('please input network address with the prefix: ')
        network = ipaddress.ip_network(network)
        print(network)

    except ValueError:
        print('That is not a network address')

    iplist= list(ipaddress.ip_network(network).hosts())
    for i in range(10,len(iplist),2):
        print(iplist[i])

    notepad1 = open('oddnetworks.txt','w')
    notepad1.write(iplist)



#port scan
def f2():
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    target = input("what website to scan?: ")
    def pscan(port):
        try:
            con = s.connect((target, port))
            return True
        except:
            return False
    for x in range(19,82):
        if pscan(x) > pscan(int(x)):
            print("port ",x," is open")
        else:
            print("port ",x," is closed")


def f3():
    sys.exit()


#user input
print("Please enter 'network' to calculate odd number IP address ranges")
print("Please enter 'port' to scan ports 19-81")
print("to exit this program, enter 'exit'")
print("note; due to performance issues, you may have to input your choice twice")

q1= input("please input your choice:  ")

valid_input = False
while not valid_input:
    q1 = input("please input your choice:  ")
    if q1 == "network":
        f1()
        valid_input = True
    if q1 == "port":
        f2()
        valid_input = True
    elif q1 == "exit":
        f3()
        valid_input = True
    else:
        print("incorrect input, try again")

Solution

  • You don't need to code an explicit loop yourself. print() can handle that for you:

    with open('oddnetworks.txt','w') as notepad1:
        print(*iplist, file=notepad1, sep='\n')
    

    This unpacks the list using * to pass each item in the list as a separate argument to print(). The output file is set using file, and each item is separated with a new line.

    Note also the use of with to open the file such that it is guaranteed to be closed.