Regarding your first question, there is nothing wrong with directly executing the commands in the function without using arguments/parameters in your function.
You could always add a parameter in your function definition to specify the path for example so that you can call the function and execute the command using different directories:
def task_speedtest(path):
os.system("speedtest-cli >> " + path)
def task_ping():
os.system("ping www.google.com -c5 >> " + path)
path = "/Desktop/logs"
task_speedtest(path)
task_ping(path)
Regarding your second question, yes, there is a better module to use than os.system
.
There exists an upgraded version of os.system
which is Subprocess
, according to the official Python documentation (Python 3.6):
The subprocess module allows you to spawn new processes, connect to
their input/output/error pipes, and obtain their return codes. This
module intends to replace several older modules and functions.
The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle.
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None)
Run the command described by args. Wait for command to complete, then return a CompletedProcess instance
There is even a section on how to replace os.system
with the new subprocess here:
sts = os.system("mycmd" + " myarg")
# becomes
sts = call("mycmd" + " myarg", shell=True)
I suggest you read more about the new module in the official Python documentation of Subprocess here: https://docs.python.org/3.6/library/subprocess.html