Home > Software engineering >  Command-Line requires file as a parameter, how to pass a variable from a Python program and write in
Command-Line requires file as a parameter, how to pass a variable from a Python program and write in

Time:06-27

Say, I have command line utility, where output file is mandatory argument:

process_data <output file>

What I want is to be able to run this CMD command from Python code, but instead of storing result to file store it into Python variable (kinda redirect it):

variable = None  # currently empty 

os.system(f"process_data {variable}")

By the end of the utility runtime I wish to get content into that variable, that was supposed to be written to the "output file":

print("output: ", variable)
output: "Data is processed successfully, here is the content of processing: ..."

CodePudding user response:

A cleaner way to call an external program is to use python subprocess module. And you have two main way of using it.

If you just want to call the program and wait for the output without catching it:

import subprocess

variable = None

subprocess.call(["process_data", variable])

The output will be printed in stdout but you will not get it in a python variable.
However you can also specify not to show the output to the user using additional arguments.

If you want to store the output in a variable:

import subprocess

variable = None

out = subprocess.check_output(["process_data", variable])

EDIT: in my example, variable is an argument to process_data, not the variable to store the output in

CodePudding user response:

Solution is here:

variable = None

response = subprocess.run(["process_data", "/dev/stdout"], capture_output=True)


variable = response.stdout

In case of text output I'd recommend to add "text=True" flag:

response = subprocess.run(["process_data", "/dev/stdout"], capture_output=True, text=True)

It is good to have a "timeout=..." flag, in case your cmd is hung:

response = subprocess.run(["process_data", "/dev/stdout"], capture_output=True, text=True, timeout=10)

Don't forget to always check on:

if response.returncode != 0:
          # do something :)
  • Related