[python-win32] os.system problem

Lawrence W. Leung larryl at imail.EECS.Berkeley.EDU
Wed May 7 11:53:33 EDT 2003


Hi,

I'm trying to use os.system to execute a command in win2k with python
2.2.2 and it's not behaving as expected.  Basically, the idea is I pass it
a command line that begins with a quoted path to an executable (to avoid
the problem of the space in the path) and also a quoted argument and it
fails to find the executable.  But oddly enough when I unquote the
argument it seems to find the executable just fine.

I've been playing with this for quite a while now and I'm starting to
suspect it's a python bug.  When I print out my command, it shows up fine.
I can even copy that into a cmd.exe window and have it run fine.  But when
I run it with python and it doesn't work.  Can someone try to reproduce
this problem to check if it's really a bug?

Here's a simplified test case using cygwin's grep:
1. copy grep.exe into a new folder "test folder" in your cygwin dir.
2. in python, os.chdir() into the cygwin dir.
3. os.system('test folder/grep.exe') returns 1 for me (can't find exe)
4. os.system('"test folder/grep.exe"') returns 128 for me (grep can't find
a cygwin dll
5. os.system('"test folder/grep.exe" --?') returns 128 for me (as
expected)
6. os.system('"test folder/grep.exe" "bleh"') returns 1 for me!! (command
not found!)

Thanks,
----
-Larry

On Tue, 6 May 2003, Raymond Hettinger wrote:

> Hello Larry,
>
> Using quotes and escape characters can work but that is
> taking the hard around.
>
> I recommend several things that will simplify solving problems
> like this.
>
> First, get the string logic right (verified with a print
> statement) before adding the os.system command and trying
> to guess what was sent to the interpreter.
>
> Second, debug your string logic using the interactive interpreter.
> This is the easiest way to see what works and avoids having
> to use print to see what is going on.
>
> Third, avoid escape character issues by using either:
>    - raw strings:   r"the \quick"
>    - outer quotes of a different type  'he said hello" to me'
>    - triple quotes """Here are the "do's and don'ts" of quoting"""
>
> Taken together the three ideas ought to make the problem trivial:
>
> >>> command = 'c:/program files/utils/grep "'
> >>> command
> 'c:/program files/utils/grep "'
> >>> argument = 'search text'
> >>> argument
> 'search text'
> >>> fullcommand = command + argument + '"'
> >>> fullcommand
> 'c:/program files/utils/grep "search text"'
> >>> import os
> >>> os.system(fullcommand)
>
>
>
> Raymond Hettinger
>
>
>
> ----- Original Message -----
> From: "Lawrence W. Leung" <larryl at imail.CS.Berkeley.EDU>
> To: <help at python.org>
> Cc: "Lawrence W. Leung" <larryl at imail.CS.Berkeley.EDU>
> Sent: Tuesday, May 06, 2003 5:34 PM
> Subject: Re: [Python-Help] os.system problem
>
>
> > Sorry, I think you misunderstood me.
> >
> > The full code to build the command looks something like this:
> > COMMAND = "\"C:/Program Files/...exe\""
> > os.system(COMMAND+" \"argument\"")
> >
> > It shouldn't matter that I'm using quotes and escape chars right?
> > Thanks,
> > -Larry
> >
> > > > I'm using 2.2.2 on windows and I noticed that os.system doesn't
> > > > seem to be working like a command line would.
> > >
> > > > If i call: "C:/Program Files/...some.exe", it works
> > > > If i call: "C:/Program Files/...some.exe" some arguments, it works
> > > > but if I call: "C:/Program Files/...some.exe"
> > > > "some-argument-in-quotes",
> > > > it complains about not being able to find the executable C:/Program
> > >
> > > Python can use either single or double quotes (actually, it can use
> > > triple-quotes too). So something like:
> > >
> > > cmd='c:/Program Files/wibble.exe "args"'
> > > os.system(cmd)
> > >
> > > ought to work.
> > >
> > > Regards,
> > > Matt
> > >
> > >
> >
> >
> > _______________________________________________
> > Python-Help maillist  -  Python-Help at python.org
> > http://mail.python.org/mailman/listinfo/python-help
>
> #################################################################
> #################################################################
> #################################################################
> #####
> #####
> #####
> #################################################################
> #################################################################
> #################################################################
>





More information about the Python-win32 mailing list