[python-win32] os.system problem

Lawrence W. Leung larryl at imail.EECS.Berkeley.EDU
Wed May 7 14:23:47 EDT 2003


I've tested it on another machine with the same python 2.2.2 version and I
still get the problem with os.system.

I'm using the version of python off python.org which doesn't appear to be
activestate's:
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Here is what I did after copying grep.exe into "test folder":
>>> os.system('"test folder/bleh"')
0
>>> os.system('"test folder/grep.exe"')
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
2
>>> os.system('"test folder/grep.exe" --?')
test folder/grep: unrecognized option `--?'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
2
>>> os.system('"test folder/grep.exe" "--?"')
0


Just to verify that it isn't grep's fault I tried it in cmd:
C:\cygwin>"test folder/grep.exe" "--?"
test folder/grep: unrecognized option `--?'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.


Perhaps the build linked off python.org has a bug??

Thanks,
--------------
-Larry

On Wed, 7 May 2003, Jens B. Jorgensen wrote:

> Things seem to work fine for me:
>
> jbj1 at tallan2 /c/temp
> $ mkdir /c/cygwin/test\ folder
>
> jbj1 at tallan2 /c/temp
> $ cp /usr/bin/grep.exe /c/cygwin/test\ folder/
>
> jbj1 at tallan2 /c/temp
> $ python
> ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on
> Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import os
>  >>> os.chdir('/cygwin')
>  >>> os.system('test folder/grep.exe')
> 0
>  >>> os.system('"test folder/grep.exe"')
> Usage: grep [OPTION]... PATTERN [FILE]...
> Try `grep --help' for more information.
> 2
>  >>> os.system('"test folder/grep.exe" --?')
> grep: unknown option -- ?
> Usage: grep [OPTION]... PATTERN [FILE]...
> Try `grep --help' for more information.
> 2
>  >>> os.system('"test folder/grep.exe" "bleh"')
> 0
>  >>>
>
> Are you using cygwin's python or ActiveState's?
>
> Lawrence W. Leung wrote:
>
> >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
> >>>
> >>>
> >>#################################################################
> >>#################################################################
> >>#################################################################
> >>#####
> >>#####
> >>#####
> >>#################################################################
> >>#################################################################
> >>#################################################################
> >>
> >>
> >>
> >
> >
> >
> >_______________________________________________
> >Python-win32 mailing list
> >Python-win32 at python.org
> >http://mail.python.org/mailman/listinfo/python-win32
> >
> >
>
> --
> Jens B. Jorgensen
> jens.jorgensen at tallan.com
>
> "With a focused commitment to our clients and our people, we deliver value through customized technology solutions"
>
>
>




More information about the Python-win32 mailing list