execute a shell script from a python script

S

spec

Hi all, I know nothing about Python. What I need to do is to get a
Python script to execute a local shell script. I do not need any
output. What would be th eeasiest way to accomplish this?

Thanks!
 
S

spec

Thanks, actually there are no args, is there something even simpler?

Thanks
Frank


Thomas said:
If your script is foo.sh and takes args:
import subprocess
subprocess.call(["foo.sh","args"],shell=True)
Should work fine. check out
http://www.python.org/dev/doc/maint24/lib/module-subprocess.html

Enjoy,
THN
Hi all, I know nothing about Python. What I need to do is to get a
Python script to execute a local shell script. I do not need any
output. What would be th eeasiest way to accomplish this?

Thanks!
 
J

John McMonagle

Thanks, actually there are no args, is there something even simpler?

Thanks
Frank


Thomas said:
If your script is foo.sh and takes args:
import subprocess
subprocess.call(["foo.sh","args"],shell=True)
Should work fine. check out
http://www.python.org/dev/doc/maint24/lib/module-subprocess.html

Enjoy,
THN
Hi all, I know nothing about Python. What I need to do is to get a
Python script to execute a local shell script. I do not need any
output. What would be th eeasiest way to accomplish this?

Thanks!

Check out os.popen4 or the commands module.
 
S

Simon Forman

spec said:
Thanks, actually there are no args, is there something even simpler?

Thanks
Frank

you could try os.system()
From the docs:

system(command)
Execute the command (a string) in a subshell. This is implemented
by calling the Standard C function system(), and has the same
limitations. Changes to posix.environ, sys.stdin, etc. are not
reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded
in the format specified for wait(). Note that POSIX does not specify
the meaning of the return value of the C system() function, so the
return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell
after running command, given by the Windows environment variable
COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always
0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status
of the command run; on systems using a non-native shell, consult your
shell documentation.

Availability: Macintosh, Unix, Windows.
 
C

Cameron Laird

you could try os.system()


system(command)
.
[more detail]
.
.
I'm concerned the follow-ups in this thread have been too subtle.
Here is what you need to know: use system(). A model such as

import os
os.system("my_script")

fulfills exactly the requirements the original poster described.
 
T

Thomas Nelson

As described in the docs I pointed to before:
subprocess.call("foo.sh",shell=True)
Is the way to do it without args. I think it is simplest to learn the
subprocess module because (quoting from the docs) this module intends
to replace several other, older modules and functions, such as:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
This way you only need to learn one thing. Actually I would like to
see some of these older functions deprecated.

THN

Cameron said:
you could try os.system()


system(command)
.
[more detail]
.
.
I'm concerned the follow-ups in this thread have been too subtle.
Here is what you need to know: use system(). A model such as

import os
os.system("my_script")

fulfills exactly the requirements the original poster described.
 
C

Cameron Laird

As described in the docs I pointed to before:
subprocess.call("foo.sh",shell=True)
Is the way to do it without args. I think it is simplest to learn the
subprocess module because (quoting from the docs) this module intends
to replace several other, older modules and functions, such as:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
This way you only need to learn one thing. Actually I would like to
see some of these older functions deprecated.
.
.
.
A point worth repeating, and I salute your courtesy in doing so.
I had realized neither the deprecation of these interfaces, nor
the documentation to that effect, so I thank you for pointing
them out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top