正在将 python 字符串输出导出到记事本

exporting python string output to notepad

我正在尝试将第 19 行和第 20 行的字符串输出导出到记事本。

代码部分,第 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)

这是我的输出,它是一系列奇数 IP 地址。请向下滚动到输出的底部以查看错误。

我收到的错误是,TypeError:write() 参数必须是 str,而不是列表我不知道如何将第 21 行转换为字符串。

输出

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

我想要这个程序,将奇数主机 IP 地址的输出保存到记事本文件中。

如有任何帮助,我们将不胜感激:)

用于测试的完整代码

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")

如错误所述,您无法将列表写入文本文件。您必须遍历列表并逐行写入文本文件。 把notepad1.write(iplist)改成

for ip in iplist:
    notepad1.write(f"{ip}\n")

您可以像这样在 for 循环中写入文件

iplist= list(ipaddress.ip_network(network).hosts())
notepad1 = open('oddnetworks.txt','w')
for i in range(10,len(iplist),2):
   print(iplist[I])
   notepad1.write(iplist)

# Don't forget to close!
notepad1.close()

您不需要自己编写显式循环代码。 print() 可以为您处理:

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

这使用 * 解压列表,将列表中的每个项目作为单独的参数传递给 print()。输出文件使用 file 设置,每一项用新行分隔。

另请注意使用 with 来打开文件以确保它被关闭。

我已经解决了,感谢您的回复。我会用本站用户提供的代码进行实验,让我的程序变得更好。

但我可以使用 str(iplist)) 输出到记事本

例如:

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