0

Im trying to record docker stats for every file in the mydata directory. For example if one of the files is names piano.txt I would like the output file to be piano_stuff.txt. This is what I have so far:

import subprocess
import signal
import os 

for file_name in os.listdir('mydata'):
    data_txt = "./" + file_name.split(".")[0] + "_stuff.txt"
    dockerStats = subprocess.Popen("docker stats  --format {{.MemUsage}} >> ${data_txt}", shell=True)
    os.killpg(os.getpgid(dockerStats.pid), signal.SIGTERM) 
cat
  • 1
  • 1
  • The question is not clear. What I can probably grab is that you want the first argument of `Popen` to be a string based on the variable? – Amit Singh Mar 10 '22 at 03:04
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 10 '22 at 06:48
  • One option is to use string formatting, like [f-strings](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings) or [`str.format`](https://docs.python.org/3/library/stdtypes.html#str.format). – 0x5453 Mar 10 '22 at 13:59
  • No, he should not be using string formatting to construct a shell command. – chepner Mar 10 '22 at 14:00
  • The question is how to include the value of `data_txt` in the shell command being constructed. – chepner Mar 10 '22 at 14:01

1 Answers1

1

Don't use shell=True. Open the file locally, and pass the file object as the stdout argument. You can also use the --no-stream option to have the command exit after producing one line of output, rather than asynchronously trying to kill the process as soon as possible. (You might get multiple lines of output, or you might get none, depending on when the OS schedules the Docker process to run.)

with open(data_txt, "a") as f:
    subprocess.run(["docker", "stats", "--format", "{{.MemUsage}}", "--no-stream"], stdout=f)
chepner
  • 497,756
  • 71
  • 530
  • 681