0

I made a simple project in python that pings a server every few seconds and I want to store the ping data in a .txt file. (It might also be cool to put it in a GUI but I need it in a txt file for now). Also, it just shows the ping in the terminal so I have no idea how I would make it go into a txt because I'm new at coding.

(here's my code btw)

import os
import time
while 1:
    os.system('ping 1.1.1.1 -n 1')
    time.sleep(5)

I didn't try much because I couldn't figure out anything I looked up stuff and nothing was what I wanted.

(also I'm a noob at coding anyways)

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94

1 Answers1

0

You'll have to run your code with Popen instead of os.system (which is a bad idea in most cases, anyway, for security reasons).

With Popen (python.org -> documentation is your friend!) you can capture the output of the programs you run. You can then write that to a file object. (That's a built-in type in python. Again, official documentation on this is good and comes with examples!)

I honestly don't see a reason to write the results of ping to a file. Wouldn't you just care about whether that ping worked and was reasonably fast? Maybe extract that information instead and just log it instead!

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • I just want to store it to see what time of day my internet is good or bad because I have rlly crappy internet and just want to see when it's unplayable for gaming. (thanks btw) – SpinierYard212 Oct 27 '22 at 23:03
  • Yeah then maybe only save the current time when it's bad. You don't care about the thing ping actually outputs. You care about the times when that output signifies a bad condition, so save nothing else. – Marcus Müller Oct 27 '22 at 23:06