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')
3
  • how are you running this script? you never closed the file! please use the with open statement to open files instead. Commented Jul 15, 2019 at 10:05
  • 1
    Why not subprocess? It does essentially the same thing, with more control. Commented Jul 15, 2019 at 10:24
  • The question has been closed but here's another post with an answer that might satisfy the OP: stackoverflow.com/questions/3503879/… Commented Jul 15, 2019 at 10:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

My code works but it does not hide the ping results. I want to hide the ping results.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.