Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execute unix shell command with python

Status
Not open for further replies.

dabayi

Programmer
Oct 23, 2002
10
DE
Hallo!

Newbie question.
How can I call the shell and execute commands on it from python?

Thanks for helping

james
 
The os module provides much of what you'd probably need. Have you checked it out?

-- Ed
-- Get great VFP support in a new forum filled with Microsoft MVPs!
--
 
Here's an example:

import os
os.system("rm badfile.txt")

If you need to see the output from your
command, try using the os.popen functions.
 
Thanks a lot. It is working!
I have another question. But let me before explain what I want to do.
With python I would like to move a file (just an example) using the shell. I know I can’t do something like that.

Code:
#!/usr/bin/env python

import os
import string

fromURL = “/home/mvT/mvtext.txt”
toURL = “/home/mvT/local/mvtest.txt”
os.popen(mv fromURL toURL)
I will get this error message.

“Error: Couldn’t find file ’fromURL’”

And I understand why. The shell tried to locate fromURL as a file.

Now the question. It is possible to export a variable with it’s value from python to the shell?

Thx in advance for helping


james
 
Three points:

(1) To make your above code work, you need to realize that the os.popen() function takes one argument, the string to be executed. (Actually, it takes 2 other optional arguments (type "help(os.popen)" for details), but you're not using those.) What you typed was:
os.popen(mv fromURL toURL)
which is actually not meaningful syntax. Somewhat closer to waht you want would be this:
os.popen("mv fromURL toURL")
where you pass in a single string. The problem here is that the string you pass has the actual letters "f", "r", "o", "m", "U", "R", "L" (and so on) instead of the VALUE of the VARIABLE you had created. So use Python's "%" operator for sticking the values of variables into strings:
os.popen( "mv %s %s" % (fromURL, toURL) )
Now the final change I would make would be to use os.system() instead of os.popen() since you don't particularly care about seeing the output (not with a mv command). So my final version would be this:

#!/usr/bin/env python
import os
# import string -- This line not needed

fromURL = "/home/mvT/mvtext.txt"
toURL = "/home/mvT/local/mvtest.txt"
os.system( "mv %s %s" % (fromURL, toURL) )


(2) Why use shell commands for this kind of work? If moving a file is really what you're after, then use the python commands for that:

import os
fromURL = "/home/mvT/mvtext.txt"
toURL = "/home/mvT/local/mvtest.txt"
import.rename( fromURL, toURL )

(3) This forum is fine, but you should consider checking out the Python Tutorial list (found at The folks there are VERY good at helping new Python programmers find their legs.
 
Well!

I took your answer with a very particular attention and I have to say that it was useful. The thing is that ‘mv’ was just an example and I have to recognize that I did not really and clearly explain my idea. What I’m really after is to know if I can pass variable from python to shell or from shell to python.
I would like to be able to give shell variables (that contain file’s path for example) and to get back from him others variables.

thx a lot

james

 
Ah... I'm not surprised to hear that "mv" was just an example. Fine... then you probably DO want to use os.popen(), and you certainly don't want to use idea (2). But my example of using Python's "%" operator to interpolate values into strings is probably what you're looking for.

When you start up the sub-shell, the only two things you can pass to it is are the command line and the environment. So, for instance, writing the script to expect two parameters, then passing them like this:

os.popen( "scriptname %s %s" % (var1, var2) )

is a great solution. I actually have no idea how to set things in the environment of a sub-shell... but I would recommend against that approach anyhow.

Basically, if you can tell me how I would pass the values to the script if I invoked it from the command line, then I can explain how to achieve it in Python.

-- Michael Chermside
 
Well! Well!
Very nice from you to help me. Can you advise me about a very good python’s tutorial book? I ‘m not really a newbie programmer. I have knowledge with other languages like Visual Basic and Java.
Thx.
Back now to python. Here is what I want to do:
- save (with python) a file from a URL given by the user on linux server. I need therefore two URLs. The URL given by the user and where I have to save the file on my linux server.
- I have to convert the file to *.txt file (if isn’t it already). I will use another application to achieve this goal. But only from the shell. That why I want to pass the shell a variable within the saved file path.
It must look like … (I just describe the cases)

Code:
#! /usr/bin/env/ python
import os
from os import *
import string

# Print out the menu
# Save the user’s URL in a variable.
user_url = raw_input (“type the file URL: “)

# Use this variable to save the file on server
	… (Need help on this one)

# Convert the file to txt using the converter application
# The converter application “capp” need two parameters that are path1 path2
# It is working like follow…
# capp path1 path2
#I think your approach to use the python’s “%” operator is very good. And use 
# “os.system” instead “os.popen” is also good.
# I tried something like that and it’s working
	fromURL=”usr/local/TestOriginal.ext”
	toURL=”usr/local/ConvertedTest.txt”
	os.system(“capp %s %s” %(fromURL,toURL))

The new question now! Can I using python execute more than one command with the shell?
For example: I can directly from the shell do this
Code:
$ var1=variable1
$ echo $var1 
variable1

I have tried this with python that unfortunately exit the shell after one command a run a new shell’s session for the following.
Code:
       os.system(“var1=variable!”)
       os.system(“echo $var1”)
       -Empty line-

How can I execute with python many commands with only on shell’s session?
Thanks a lot

james







 
Can I using python execute more than one command with the shell?

Well, there's this:
Code:
os.system( "command_one" )
Code:
os.system( "command_two" )
which is easy, and works fine. But I presume you already thought of that. How to do multiple commands depends on what shell you have. Some would like this:
Code:
os.system( "command_one; command_two" )
Others prefer this:
Code:
os.system( "command_one\ncommand_two" )
And some are even wierder. But I'm assuming you know your own shell.

As for tutorials, I'm not an expert on it, but I'd suggest the (previously mentioned) python-tutor list (you can read it on the web without subscribing). One obvious spot is the official tutorial on python.org ( but I know that there are several other good ones available.

-- Michael Chermside
 
Great! Perfect with the commands. I just have to say T H A N K S and go back to my tutorial. (I get "dive into python") it seems to be good.
Thanks one more for all

James
 
No problem! Next time it'll probably be me with a question YOU have the answer to. These things come around.

-- Michael Chermside
 
to run a command as if it were from the shell, do the following:

os.spawnvpe('P_WAIT','foo',foo_lst,os.environ)

the os.spawnvpe tells python to start a child process. 'P_WAIT' is saying to python, "wait until the child process is done before killing it". the 'foo' is a name that you give to the process. foo_lst is the commands you want execute. ex:

foo_lst=['/bin/tar','cvf','/usr/backup/backup.tar','/home','/etc']

os.eviron tells python to use the current $PATH variables.

i look back, and it took me FOREVER to learn how to do that.
lol. so i thought i'd pass it along into the public.

have fun!

python rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top