-4
Perform a function that reads a text file containing a list of IP addresses and generates two other files, one containing valid IP addresses and the other containing
invalid addresses. The format of an IP address is num1.num.num.num
, where num1
from 1 to 255 and num
goes from 0 to 255.
def ips(arq1):
ipsv=open('ipsv.txt','w')
ipsi=open('ipsi.txt','w')
c=0
junta=""
for ip in arq1:
listaip = []
for i in range(len(ip)):
if ip[i]!=".":
junta=junta+ip[i]
junta=(junta)
if len(junta)==3:
listaip.append(int(junta.strip()))
junta=""
else:
continue
print(listaip)
for x in listaip:
if listaip[0]>=1 and listaip[0]<=255 and x>=0 and x<=255:
ipsv.write(str(x))
else:
ipsi.write(str(x))
ipsi.close()
ipsv.close()
arq1=open('ips.txt','r')
ips(arq1)
And what is the mistake? Which line?
– Ricardo Pontual
File "C:/Users/Lucas/Pycharmprojects/Uff/file/5.py", line 21, in ips listaip.append(int(junta.strip()) Valueerror: invalid literal for int() with base 10: '0 n1'
– Lucas Soares
The error message was very clear: there is a character
\n
in the middle of your string that cannot be converted to integer.– Woss