16

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.

oguz ismail
  • 1
  • 16
  • 47
  • 69
user3003701
  • 169
  • 1
  • 1
  • 3

9 Answers9

33

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
''')
oguz ismail
  • 1
  • 16
  • 47
  • 69
Anynomous
  • 347
  • 3
  • 3
8

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.
Muthu Kumar
  • 885
  • 2
  • 14
  • 25
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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
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
Anonymous
  • 738
  • 4
  • 14
  • 36
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

volcano
  • 3,578
  • 21
  • 28
1

Try to use subprocess.Popen and cwd

example:

subprocess.Popen('launchMyApp', cwd=r'/working_directory/')
Puffin GDI
  • 1,702
  • 5
  • 27
  • 37
  • 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" – user3003701 Nov 18 '13 at 07:34
  • If you need to get output from cmd. Maybe you can refer (http://stackoverflow.com/questions/1388753/how-to-get-output-from-subprocess-popen) – Puffin GDI Nov 18 '13 at 07:38
  • 1
    `p = subprocess.Popen( ...) ` and `line = p.stdout.readline()` – Puffin GDI Nov 18 '13 at 07:41
-1

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41
-1

Just use

os.system("first command\nsecond command\nthird 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

.

Cybersupernova
  • 1,833
  • 1
  • 20
  • 37
-1

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Fabien Thetis
  • 1,666
  • 15
  • 11