Skip to content
Advertisement

How to invoke a specific shell with python?

I want to invoke a specific command shell in python to execute some scripts. For exemple, in R, I use:

system2(os_shell = "C:/Program Files (x86)/pgAdmin III/1.18/pg_dump.exe", args = "very long argument")

Thanks to this code, I can backup my Postgresql’s tables with a for loop.

Problem: I didn’t find an equivalent in Python.

Advertisement

Answer

If you just want to execute a application with args in a shell you can do that very easily with os.system

import os
os.system("'C:/Program Files (x86)/pgAdmin III/1.18/pg_dump.exe' very long argument")

Tho I would recommend subprocess.call or subprocess.run

import subprocess
subprocess.call(["'C:/Program Files (x86)/pgAdmin III/1.18/pg_dump.exe'", "argument1", "argument2"]) #and you can add as many arguments you want
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement