17

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.

0

9 Answers 9

34

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
''')
Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

3

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.

Comments

3

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

Comments

1

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

1 Comment

Could you please share an example using subprocess?
1

Try to use subprocess.Popen and cwd

example:

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

3 Comments

I may not know the changed directory. Is there a way I could get the changed directory from the previous call ? so that I could pass that while calling to "launchMyApp"
If you need to get output from cmd. Maybe you can refer (stackoverflow.com/questions/1388753/…)
p = subprocess.Popen( ...) and line = p.stdout.readline()
-1

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

Comments

-1

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

Comments

-1

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

.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.