<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: How Can I Determine the Operating System with Python?</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>I did something mildly cheezy like this to distinguish between different windows flavours:</FONT>
</P>

<P><FONT SIZE=2>#-----------------------------------------------------------------------------</FONT>
<BR><FONT SIZE=2>class ComputerInfo:</FONT>
<BR><FONT SIZE=2>    OSnames =\</FONT>
<BR><FONT SIZE=2>    {</FONT>
<BR><FONT SIZE=2>        "Windows 2000" : "Windows2000",</FONT>
<BR><FONT SIZE=2>        "Windows 2002" : "WindowsXP",</FONT>
<BR><FONT SIZE=2>        "4.10.1998"    : "Windows98"</FONT>
<BR><FONT SIZE=2>    }</FONT>
</P>

<P><FONT SIZE=2>    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</FONT>
<BR><FONT SIZE=2>    def __init__( self ):</FONT>
<BR><FONT SIZE=2>        self.infoGet( "net config" ) # this is win98's syntax</FONT>
<BR><FONT SIZE=2>        try:</FONT>
<BR><FONT SIZE=2>            name = self.computername</FONT>
<BR><FONT SIZE=2>            osname = self.OSName</FONT>
<BR><FONT SIZE=2>        except:</FONT>
<BR><FONT SIZE=2>            self.infoGet( "net config workstation" ) # this works for win2k and xp</FONT>
<BR><FONT SIZE=2>            </FONT>
<BR><FONT SIZE=2>    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</FONT>
<BR><FONT SIZE=2>    def infoGet( self, string ):</FONT>
<BR><FONT SIZE=2>        computername = "Computer name"</FONT>
<BR><FONT SIZE=2>        username = "User name"</FONT>
<BR><FONT SIZE=2>        softwareversion = "Software version"</FONT>
<BR><FONT SIZE=2>        infofile = open( "computerinfo.txt", "wt" )</FONT>
<BR><FONT SIZE=2>        CmdRun( string, infofile )</FONT>
<BR><FONT SIZE=2>        infofile.close()</FONT>
<BR><FONT SIZE=2>        for line in fileinput.input( "computerinfo.txt" ):</FONT>
<BR><FONT SIZE=2>            if line.find( computername ) == 0:</FONT>
<BR><FONT SIZE=2>                self.computername = os.path.split( line[len(computername):].strip() )[1]</FONT>
<BR><FONT SIZE=2>            if line.find( username ) == 0:</FONT>
<BR><FONT SIZE=2>                self.username = line[len(username):].strip()</FONT>
<BR><FONT SIZE=2>            if line.find( softwareversion ) == 0:</FONT>
<BR><FONT SIZE=2>                self.softwareversion = line[len(softwareversion):].strip()</FONT>
<BR><FONT SIZE=2>                self.OSName = ComputerInfo.OSnames[self.softwareversion]</FONT>
</P>
<BR>
<BR>

<P><FONT SIZE=2>#-----------------------------------------------------------------------------</FONT>
<BR><FONT SIZE=2>def CmdRun( path, logfile=None ):</FONT>
<BR><FONT SIZE=2>    print "CmdRun: ", time.ctime( time.time() )</FONT>
<BR><FONT SIZE=2>    print "CmdRun:     ", path</FONT>
<BR><FONT SIZE=2>    (instream,outstream) = os.popen4( path )</FONT>
<BR><FONT SIZE=2>    while 1:</FONT>
<BR><FONT SIZE=2>        line = outstream.readline()</FONT>
<BR><FONT SIZE=2>        if not line:</FONT>
<BR><FONT SIZE=2>            break</FONT>
<BR><FONT SIZE=2>        try:</FONT>
<BR><FONT SIZE=2>            if logfile:</FONT>
<BR><FONT SIZE=2>                logfile.write( line )</FONT>
<BR><FONT SIZE=2>        except IOError:</FONT>
<BR><FONT SIZE=2>            print "Exception generated on '%s'" % line</FONT>
</P>

</BODY>
</HTML>