1

I want my code to take the i/p and o/p file names, ping all websites present in the i/p file, and display "pingable" or "not pingable" in the output file. It must not display any ping results. I don't want to use subprocess. All commands must support command prompt(windows).

The code is working fine but the ping results are displayed on execution. I don't want it to be displayed.

i tried to redirect the result to a document called trash.txt, but it doesn't seem to work.

import os

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

fname1 = input('Enter the file name to read: ')

fname2=input('enter filenme to write:')

finp = open(fname1,'r')

fout=open(fname2,'w')

for line in finp:

 os.system('@ECHO OFF')

 response=os.system('ping -n 1 '+line+' > trash.txt')

if response==0:

     os.system('echo ok')
     fout.write(line+'\tis pingable\n')

 else:

     fout.write(line+'\tis not pingable\n')
  • how are you running this script? you never closed the file! please use the `with open` statement to open files instead. – Paritosh Singh Jul 15 '19 at 10:05
  • 1
    Why not subprocess? It does essentially the same thing, with more control. – ja2142 Jul 15 '19 at 10:24
  • The question has been closed but here's another post with an answer that might satisfy the OP: https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on – asikorski Jul 15 '19 at 10:28

1 Answers1

0

I don't know if that would help you and is your problem, but your ping line is having an Invalid Argument problem.

Instead of using response=os.system('ping -n 1 '+line+' > trash.txt') you may use response=os.system('ping '+line+' -n 1 > trash.txt') or sth like that.

Alireza HI
  • 1,873
  • 1
  • 12
  • 20