Aug-12-2023, 06:11 AM
readlines()
splits it into separate lines,but also add new line \n
.Change to this.
with open("ip_list.txt") as file: park = [ip.strip() for ip in file]Or in your first post you try to scan a folder for ip_list text files,then it can be done something like this.
import time import subprocess from concurrent.futures import ThreadPoolExecutor import pandas as pd import os def ping(ip): return ( ip, subprocess.run( f"ping {ip} -n 1", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ).returncode, ) start = time.time() def scan_files(): directory = '.' for entry in os.scandir(directory): if entry.is_file() and entry.name.endswith('.txt'): if 'ip_list' in entry.name: pt = directory + '/' + entry.name with open(pt) as file: for ip in file: yield ip.strip() executor = ThreadPoolExecutor(5) df = pd.DataFrame(executor.map(ping, list(scan_files()))) df.to_csv(r'ip_output.csv',header=False, index=False, quoting=None) print(df)