[python-win32] os.system problem

Scott Prive scottprive at earthlink.net
Thu May 8 09:27:18 EDT 2003


I should have read the entire thread before replying.....

My "use unix pathing" advice goes if you were using Cygwin Python. You're
not. (You can have both on your system and they will not conflict, as long
as you don't put Cygwin stuff in your PATH... which I don't reccomend)

The versions of Python from python.org and Activestate both understand
windows-style pathing. There will be no differences there, but again, Cygwin
utilities might behave strange. A dummy-script to dump what you pass it will
be helpful I believe.




----- Original Message ----- 
From: "Scott Prive" <scottprive at earthlink.net>
To: "Lawrence W. Leung" <larryl at imail.EECS.Berkeley.EDU>;
<python-win32 at python.org>
Sent: Thursday, May 08, 2003 8:21 AM
Subject: Re: [python-win32] os.system problem


> Been there :-)
>
> Lawrence, try this:
>
> 1) Know that Cygwin executables will only understand UNIX-style pathing:
> /path/to/foobar
> You CAN invoke these utilities from a CMD.EXE shell, or say Python... but
> the above is still true: things like path arguments need to be in /a/b/c
> style. Lots of users in the cygwin mailing list have fallen into this trap
> (inc. me).
>
> Use UNIX-style pathing. You can also experiment some with the Cygwin
utility
> "cygpath", but you'll need to be creative and find a solution using it.
>
> 2) This really helped me:
>
> Create a dummy shell script called foo.sh. The only logic this script
should
> have is:
> echo $0 $1 $2 $3 etc.
>
> Now call this shell command from Python *exactly* how you were calling
your
> other, problematic command. You should spot the error.
>
> What's likely happening is your strings are unescaped TWICE... once in
> python, and once by CMD.EXE before it arrives at your target command (at
> least, that's how it works the way I was calling things from Cygwin Perl).
> For me I had to change my strings to (ex): "net use
> \\\\\\\\servername\\\\share X: user at domain"
>
> Also, there may be a way in Python to "directly" call commands, without
> handing the job off to a shell process. Had I done that, I would have
> avoided the shell completely and could have done: \\\\server\\share ....
>
> It's been a while and I might have a slight error in the above, but the
> technique for validating what your command "really" sees is valid, and so
is
> the escaping requirements. The dummy shell command is key because it is
very
> difficult to debug what shell commands "really" see when invoked... most
> will simply return usage errors, and not dump what it is they were asked
to
> do.
>
> -Scott
>
>
>
> ----- Original Message ----- 
> From: "Lawrence W. Leung" <larryl at imail.EECS.Berkeley.EDU>
> To: <python-win32 at python.org>
> Sent: Wednesday, May 07, 2003 1:53 PM
> Subject: [python-win32] os.system problem
>
>
> > 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
>
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32




More information about the Python-win32 mailing list