user3003701
user3003701

Reputation: 179

Calling multiple commands using os.system in Python

I would like to invoke multiple commands from my python script. I tried using the os.system(), however, I'm running into issues when the current directory is changed.

example:

os.system("ls -l")
os.system("<some command>") # This will change the present working directory 
os.system("launchMyApp") # Some application invocation I need to do.

Now, the third call to launch doesn't work.

Upvotes: 17

Views: 88408

Answers (9)

Cybersupernova
Cybersupernova

Reputation: 1839

Just use

// it will execute all commands even if anyone in betweeen fails
os.system("first command;second command;third command")
// or, it will try to execite but will stop as any command fails
os.system("first command && second command && third command")

I think you have got the idea what to do

Note: This is not a very reliable approach if you are doing a complex job using CLI tools. Popen and subprocess methods are better there. Although small task link copy, move, list will work fine

.

Upvotes: -1

Anynomous
Anynomous

Reputation: 357

os.system is a wrapper for the C standard library function system(). Its argument can be any valid shell command as long as it fits into the memory reserved for environment and argument lists of a process.

So, delimit each command with a semicolon or a newline and they will be executed one after another in the same environment.

os.system(" ls -l; <some command>; launchMyApp")
os.system('''
ls -l
<some command>
launchMyApp
''')

Upvotes: 34

Holy Mackerel
Holy Mackerel

Reputation: 3279

You can change back to the directory you need to be in with os.chdir()

Upvotes: -1

Fabien Thetis
Fabien Thetis

Reputation: 1734

os.system("ls -l && <some command>")

Upvotes: -1

Anonymous
Anonymous

Reputation: 754

It’s simple, really. For Windows separate your commands with &, for Linux, separate them with ;.
str.replace is a very good way to approach the problem, used in the example below:

import os
os.system('''cd /
mkdir somedir'''.replace('\n', ';')) # or use & for Windows

Upvotes: 3

Muthu Kumar
Muthu Kumar

Reputation: 945

Try this

import os

os.system("ls -l")
os.chdir('path') # This will change the present working directory
os.system("launchMyApp") # Some application invocation I need to do.

Upvotes: 9

jfs
jfs

Reputation: 414139

Each process has its own current working directory. Normally, child processes can't change parent's directory that is why cd is a builtin shell command: it runs in the same (shell) process.

Each os.system() call creates a new shell process. Changing the directory inside these processes has no effect on the parent python process and therefore on the subsequent shell processes.

To run multiple commands in the same shell instance, you could use subprocess module:

#!/usr/bin/env python
from subprocess import check_call

check_call(r"""set -e
ls -l
<some command> # This will change the present working directory 
launchMyApp""", shell=True)

If you know the destination directory; use cwd parameter suggested by @Puffin GDI instead.

Upvotes: 3

Puffin GDI
Puffin GDI

Reputation: 1702

Try to use subprocess.Popen and cwd

example:

subprocess.Popen('launchMyApp', cwd=r'/working_directory/')

Upvotes: 1

volcano
volcano

Reputation: 3582

When you call os.system(), every time you create a subshell - that closes immediately when os.system returns (subprocess is the recommended library to invoke OS commands). If you need to invoke a set of commands - invoke them in one call. BTW, you may change working director from Python - os.chdir

Upvotes: 1

Related Questions