Created for
subprocess
module
# execute 'date +%H:%M:%S' by Python:
>>> import subprocess
>>>
>>> subprocess.call(["date", "+%H:%M:%S"])
0
# execute 'date +%H:%M:%S' and get output:
>>> import subprocess
>>> output = subprocess.check_output(["date", "+%H:%M:%S"])
>>> output
b'15:33:16\n'
# decode the bytestring to ascii
>>> output_ascii = output.decode('ascii')
>>> output_ascii
'15:33:16\n'
# strip leading and trailing spaces:
>>> output_ascii.strip()
'15:33:16'
Open a terminal with a custom profile in the given directory
import subprocess
def open_terminal(profile, directory):
"""open a terminal with a custom profile in the given directory"""
cmd = "gnome-terminal"
args = (
"--window-with-profile",profile,
"--working-directory",directory
)
subprocess.call([cmd, *args])
open_terminal("day", "/data/python_demos/music")
open VS Code editor and load given directory:
import subprocess
def open_vscode(directory):
"""open VS Code editor and load given directory"""
cmd = "code"
subprocess.call([cmd, directory])
open_vscode("/data/python_demos/music")
import subprocess
def load_browser(profile, url):
"""open Chrome browser in new window,
with the given user profile and loads the specified URLs
"""
cmd = "google-chrome"
args = "--new-window --profile-directory=" + "'{:s}' {:s}".format(profile, url)
subprocess.call(cmd + " " + args, shell=True)
urls_to_load = [
"https://mail.google.com",
"http://wwwcourses.github.io/ProgressBG-Python"
]
load_browser("Profile 7", " ".join(urls_to_load))
These slides are based on
customised version of
framework