os.execvp() is giving me trouble
Jonathan Crowell
jonathan.crowell at db.com
Fri Apr 7 14:16:23 EDT 2006
Hi All,
Sorry if this is a newbie question. I promise I have RTFMed, though.
Here goes:
I'm trying to invoke an external program from python. The program is
jar.exe, which is part of java. The following is the command I want to
send:
jar -xvf file1.jar jile2.jar file3.jar
os.execvp(file, args) is described as follows:
execvp(file, args)
Execute the executable file (which is searched for along $PATH)
with argument list args, replacing the current process.
args may be a list or tupe of strings.
I have a list of strings that looks like this:
jarfiles = ['c:\\documents and settings\\myname\\mydir\\file1.jar',
'c:\\documents and settings\\myname\\mydir\\file2.jar', 'c:\\documents and
settings\\myname\\mydir\\file3.jar']
The jar command takes some arguments. I would like to give it the
arguments "-xvf". I have tried both of the following options:
1) inserting "-xvf" as the first element in the list above and then
calling os.execvp("jar", jarfiles);
2) directly calling without the list, as in os.execlp("jar", "-xvf",
"c:\\documents and settings\\myname\\mydir\\file1.jar", "c:\\documents and
settings\\myname\\mydir\\file2.jar", "c:\\documents and
settings\\myname\\mydir\\file3.jar");
Neither of these options seems to work. The output looks like the
following:
C:\Documents and Settings\mydir\pythonscripts>Illegal option: :
Usage: jar {ctxu}[vfm0Mi] [jar-file] [manifest-file] [-C dir] files
(more usage output here...)
If anyone knows how I can invoke a command and pass it a list of
arguments, that would be helpful.
By the way, I know that the pathnames of my jar files have spaces in them,
so I have also tried enclosing them in double quotes by adding a double
quote to the begining and end of every element in my list of jar files. I
still got a very similar error. I also tried changing the paths to
relative paths that didn't contain spaces and I still got a very similar
error.
Thanks,
Jon
---------- complete source ---------
import os;
import glob;
# define some useful directories we will be using in this script.
home_dir = os.path.normcase(os.environ['USERPROFILE']);
dist_dir = os.path.normcase(home_dir+"/mydir/dist/");
other_dir = os.path.normcase(home_dir+"/otherDir/");
os.chdir(home_dir);
# get all the jar files in the mydir distribution directory
jarfiles = glob.glob(dist_dir+"\\*.jar");
# delete every directory and file in the otherDir directory
for root, dirs, files in os.walk(other_dir, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
# make the empty otherDir directory the current working directory
os.chdir(other_dir);
# for each jar file, extract it into the otherDir directory
for jarfile in jarfiles:
os.execlp("jar", "-xvf", jarfile);
---------- end source ----------
(I also tried using execvp and a list of arguments (all the jarfiles) but
that didn't work either.)
--
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060407/78a710ec/attachment.html>
More information about the Python-list
mailing list