1

How can I run an external program automatically using Python script?

I installed a program that can analyze demographics. Let's say the program name is Demograph I am doing the following commands in the Windows command prompt to extract specific people's information.

Demograph hobby Baseball FileA > OutputfileA

FileA contains people's information. And this command can extract people who like baseball. I can change the baseball to soccer, swimming, etc. This command generates an OutputfileA. As the next step, I am doing the following command to further analyze the OutputfileA.

Demograph age 10-15 OutputfileA > OutputfileA2

This command can extract people's information whose ages are 10-15 from OutputfileA and then generate the OutputfileA2

However, typing this information on the command prompt every time is a pain. I want to do this automatically using Python script. Should I use "subprocess" to run the external program automatically?

Tom_Hanks
  • 517
  • 1
  • 6
  • 15
  • Quite a few more SO Q&A's to peruse searching with variations of `python subprocess execute a program write result to a file site:stackoverflow.com` – wwii May 16 '21 at 14:55

2 Answers2

0

Yeah, subprocess is really great for it. Simple use it:

import subprocess
subprocess.call([])

If you want to parse output, read about STDout pipes and ect in DOC: https://docs.python.org/3/library/subprocess.html

r1v3n
  • 432
  • 4
  • 9
0

Yes, you can use subprocess and this is an example:

import subprocess

subprocess.Popen("Demograph hobby Baseball FileA > OutputfileA")

# rest of your code
Arib Muhtasim
  • 147
  • 2
  • 8