-2

i am trying to run some command line in python script, like:

ping 8.8.8.8

and when i write this code:

import os
os.system('ping 8.8.8.8')

it printing me the details of the command. but i want it for be background command, what mean i dont want to user will see all the details. i want to save it to varilble, and after that edit and print as my wish. how can i do that? i try to store what the function return, as:

answer = os.system('ping 8.8.8.8')

but the function return 0. help someone?

J. doe
  • 13
  • 4
  • use `subprocess.check_output()` instead – Anthon Jul 09 '17 at 16:45
  • 5
    Possible duplicate of [Assign output of os.system to a variable and prevent it from being displayed on the screen](https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on) – Craig Jul 09 '17 at 16:46

2 Answers2

0

Simply create a bat file with ping 8.8.8.8 written in it. Then you can use the subprocess command to invoke it from within the python script.

import subprocess
subprocess.Popen('ping.bat')
Harshith Thota
  • 856
  • 8
  • 20
0

import os res = os.popen('ping 8.8.8.') print(res.read()) res.close()

But I think it's safer to use subprocess.Popen() instead of os.popen().
Here show how to switch over

gaback
  • 638
  • 1
  • 6
  • 13