in Education by
I am trying to write my below pinging script results into the Text file, but I am getting an error message. Below is my code: import os import time import subprocess # Open file for saving ping results results_file = open("results.txt", "w") with open('ip-source.txt') as IPs: hosts = IPs.read() hosts = hosts.splitlines() for ip in hosts: os.system('cls') print('Printing now:', ip) print('-'*60) result = os.system('ping -n 4 {}'.format(ip)) print('-'*60) time.sleep(1) with open("output.txt", "w", encoding="utf8") as output: output.write("\n".join([post.text for post in result])) and when i ran it i got below error message: Traceback (most recent call last): File "script.py", line 21, in output.write("\n".join([post.text for post in result])) Error: TypeError: 'int' object is not iterable Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
The os.system returns an exit signal (typically 0 or 1) from a process called. If you look, os.system is just the wrapper function for subprocess.Popen. You need to use something like a subprocess. Popen or subprocess.check_output and then b) add results to the list: import subprocess ... results = [] for ip in hosts: ... results.append(str(subprocess.check_output(["ping", "-n", "4", str(ip)]))) with open("output.txt", "w", encoding="utf8") as output: output.writelines(results) If you are a beginner and want to know more about Python the do check out the python for data science course

Related questions

0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have an array of the shape (100000, 1) with each element in an array of type positive integer and not ... help would be appreciated. Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I'm looking for a decent implementation of the OPTICS algorithm in Python. I will use it to form density-based ... to that cluster. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Here is my sample string: [true, {"name": "NameofItem", "amount": "1", "price": 100, "sellerName": " ... seem to make that work. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is better than R for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is used in Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is necessary for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me whether Python is good for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am starting to work with python again after 8 years. I am trying to do the program with BeautifulSoup and an ... '__main__': main() Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    When I run pip3 install -r requirements.txt on the project I get this error message: pip._vendor.pkg_resources. ... on my machine. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...