How Can I Determine the Operating System with Python?

John Schmitt jschmitt at ati.com
Thu Mar 21 13:08:19 EST 2002


I did something mildly cheezy like this to distinguish between different
windows flavours:

#---------------------------------------------------------------------------
--
class ComputerInfo:
    OSnames =\
    {
        "Windows 2000" : "Windows2000",
        "Windows 2002" : "WindowsXP",
        "4.10.1998"    : "Windows98"
    }

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



#---------------------------------------------------------------------------
--
def CmdRun( path, logfile=None ):
    print "CmdRun: ", time.ctime( time.time() )
    print "CmdRun:     ", path
    (instream,outstream) = os.popen4( path )
    while 1:
        line = outstream.readline()
        if not line:
            break
        try:
            if logfile:
                logfile.write( line )
        except IOError:
            print "Exception generated on '%s'" % line
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020321/fc6f354a/attachment.html>


More information about the Python-list mailing list