Tags: 0print, elseprint, errorif, failed, isan, ossystem, programming, python, return, returned, safe, system, value, values, win32, winver

return values of os.system() on win32

On Programmer » Python

5,248 words with 6 Comments; publish: Wed, 02 Jan 2008 18:48:00 GMT; (20062.50, « »)

Is it safe to say that any value returned by os.system() other than 0 is

an error?

if os.system('winver') != 0:

print "Winver failed!"

else:

print "Winver Worked."

Thanks!

All Comments

Leave a comment...

  • 6 Comments
    • rbt <rbt.python.todaysummary.com.athop1.ath.vt.edu> writes:

      > Is it safe to say that any value returned by os.system() other than 0 is an

      > error?

      I believe not. That depends on the return/error code of the specific program

      you ran.

      --

      Jorge Godoy <godoy.python.todaysummary.com.ieee.org

      "Quidquid latine dictum sit, altum sonatur."

      - Qualquer coisa dita em latim soa profundo.

      - Anything said in Latin sounds smart.

      #1; Wed, 02 Jan 2008 18:49:00 GMT
    • rbt wrote:

      > Is it safe to say that any value returned by os.system() other than 0 is

      > an error?

      > if os.system('winver') != 0:

      > print "Winver failed!"

      > else:

      > print "Winver Worked."

      According to the docs, assuming that *in general* would be an error, but

      it's likely that for the sorts of cases you are talking about, it's true.

      Ultimately, since the return code is generally under the control of the

      application you're calling, it's absolutely possible (likely) that there

      are many programs which do not work as you assume above, and probably a

      large number which don't ever explicitly set the return value at all...

      -Peter

      #2; Wed, 02 Jan 2008 18:50:00 GMT
    • Peter Hansen wrote:

      > rbt wrote:

      >> Is it safe to say that any value returned by os.system() other than 0

      >> is an error?

      >>

      >> if os.system('winver') != 0:

      >> print "Winver failed!"

      >> else:

      >> print "Winver Worked."

      >

      > According to the docs, assuming that *in general* would be an error, but

      > it's likely that for the sorts of cases you are talking about, it's true.

      > Ultimately, since the return code is generally under the control of the

      > application you're calling, it's absolutely possible (likely) that there

      > are many programs which do not work as you assume above, and probably a

      > large number which don't ever explicitly set the return value at all...

      > -Peter

      OK, thanks guys. That's helpful... this is more of an MS issue than a

      Python issue.

      #3; Wed, 02 Jan 2008 18:51:00 GMT
    • rbt wrote:

      > Is it safe to say that any value returned by os.system() other than 0 is

      > an error?

      > if os.system('winver') != 0:

      > print "Winver failed!"

      > else:

      > print "Winver Worked."

      > Thanks!

      What are you really seeking to do? Are you wanting to detect if your

      code is running on a Windows machine? Are you wanting to know the

      version number of Windows? Why not use popen2() and see the output?

      #4; Wed, 02 Jan 2008 18:52:00 GMT
    • Paul Watson wrote:

      > rbt wrote:

      >> Is it safe to say that any value returned by os.system() other than 0

      >> is an error?

      >>

      >> if os.system('winver') != 0:

      >> print "Winver failed!"

      >> else:

      >> print "Winver Worked."

      >>

      >> Thanks!

      >

      > What are you really seeking to do?

      This is a corner case. I'm trying to detect if the py script is running

      on a 'special' version of windows. I can't go into the details about

      what makes it unique. Python installs and runs, but the windows API

      isn't as complete as a normal Windows install... among other things, it

      doesn't have a winver.exe file, or if it does, it's crippled... this

      causes os.system('winver') to return a 1... while it returns 0 on

      Windows XP, etc.

      > Are you wanting to detect if your

      > code is running on a Windows machine? Are you wanting to know the

      > version number of Windows? Why not use popen2() and see the output?

      #5; Wed, 02 Jan 2008 18:53:00 GMT
    • [rbt wrote]

      > This is a corner case. I'm trying to detect if the py script is running

      > on a 'special' version of windows. I can't go into the details about

      > what makes it unique. Python installs and runs, but the windows API

      > isn't as complete as a normal Windows install... among other things, it

      > doesn't have a winver.exe file, or if it does, it's crippled... this

      > causes os.system('winver') to return a 1... while it returns 0 on

      > Windows XP, etc.

      If you just want to check if winver.exe exists you could just try

      os.path.exists("path\\to\\winver.exe") if you know where to expect it

      (likely "C:\Windows\system32") or you could use which.py:

      >>> import which

      >>> which.which("winver.exe")

      'C:\\WINDOWS\\system32\\winver.exe'

      >>> which.which("nothere")

      Traceback (most recent call last):

      File "<stdin>", line 1, in ?

      File "C:\Python24\which.py", line 248, in which

      raise WhichError("Could not find '%s' on the path." % command)

      which.WhichError: Could not find 'nothere' on the path.

      http://trentm.com/projects/which/

      Trent

      --

      Trent Mick

      TrentM.python.todaysummary.com.ActiveState.com

      #6; Wed, 02 Jan 2008 18:54:00 GMT