fork download
  1. import subprocess
  2. from tempfile import TemporaryDirectory
  3. from pathlib import Path
  4.  
  5. with TemporaryDirectory() as tmpdirname:
  6. with open(Path(tmpdirname) / "logfile.txt", 'w') as log_file:
  7. cmd = 'printf "%s\\n" "foo" "bar" "baz"'
  8. log_file.write("Running the command: %s\n" % cmd)
  9. log_file.flush()
  10. subprocess.call(cmd, shell=True, stdout=log_file, stderr=log_file)
  11.  
  12. with open(Path(tmpdirname) / "logfile.txt", 'r') as read_file:
  13. for line in read_file:
  14. print(line, end='')
  15.  
Success #stdin #stdout 0.05s 15996KB
stdin
Standard input is empty
stdout
Running the command: printf "%s\n" "foo" "bar" "baz"
foo
bar
baz