123456789101112131415161718192021222324252627282930313233343536 |
- import csv
- import os
- # Create folders for each first digit of IP address
- for i in range(256):
- folder_path = f"geoip/{i}"
- if not os.path.exists(folder_path):
- os.makedirs(folder_path)
- # Read CSV file and create text files
- with open('geoipv4.csv', 'r') as csvfile:
- csvreader = csv.reader(csvfile)
- for row in csvreader:
- start_ip = row[0]
- end_ip = row[1]
- country = row[2]
- first_digit, second_digit, _, _ = start_ip.split(".")
- file_path = f"geoip/{first_digit}/{second_digit}.txt"
- if os.path.exists(file_path):
- append_write = 'a' # append if already exists
- else:
- append_write = 'w' # make a new file if not
- with open(file_path, append_write) as file:
- file.write(f"{start_ip},{end_ip},{country}\n")
- # Clean empty folders
- root_dir = "geoip/"
- # Loop through all subdirectories in the root directory
- for subdir in os.listdir(root_dir):
- subdir_path = os.path.join(root_dir, subdir)
-
- # Check if the subdirectory contains any txt files
- if not any(fname.endswith(".txt") for fname in os.listdir(subdir_path)):
- # If not, remove the subdirectory
- os.rmdir(subdir_path)
|