#!/usr/bin/env python # -*- coding:utf-8 -*- import sys import socket import re def ip_list(): ip_file = input("请输入文件名称:") file = open(ip_file,'r') #打开文件 ip = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])') #筛选IP正则表达式 file1 = file.readlines() #读取文本内容(.readlines()自动将文件内容分析成一个行的列表) print(file1) for iplist in file1: for iprow in ip.findall(iplist): print(iprow) file2 = open("ip_list","a") file2.write(iprow + "\n") file2.close() file.close() if __name__ == '__main__': ip_list()