I am currently trying to split a file into a list of IPs, how would i make it run until the end of the file and then stop when it reaches the end. It currently reads the first lines splits it and stops.
with open('log1.txt') as logFile: for line in logFile: i = i+1 content_list = line.split("-") #ipList[i] = content_list # I want it to be split into this list break print(content_list)
Here is also an example of what my list looks like
172.16.121.64 - - [03/Sep/2018:09:46:19] GET /index.html HTTP/1.1 200 437 172.16.121.64 - - [03/Sep/2018:09:46:23] GET /index.html HTTP/1.1 200 437 172.16.121.64 - - [03/Sep/2018:09:46:27] GET /index.html HTTP/1.1 200 437 172.16.121.35 - - [03/Sep/2018:09:46:28] GET /index.html HTTP/1.1 200 437 172.16.121.240 - - [03/Sep/2018:09:46:31] GET /index.html HTTP/1.1 200 437
you can split the line at space and take element at index 0. You can do evevrything in list comprehension
with open('log1.txt') as logFile: ips = [line.split() for line in logFile] print(ips)
If you plan to manipulate IPs further, look at ipaddress from Standard Library.