3

Is it possible to check, using Python programming languque, the time on which the operating system is installed ? Mainly, I am interested in Windows XP platform. I wonder if there is such an API in python or any trick to do it.

Andy G
  • 19,232
  • 5
  • 47
  • 69

5 Answers5

2

This isn't python specific, but you can find this via the systeminfo and find commands.

>systeminfo | find /i "original"
Original Install Date:     7/27/2011, 3:06:49 PM

The string original applies if the installation locale is English.

You can wrap this in an os.system call

>>> os.system("""systeminfo | find /i "original" """)
Original Install Date:     7/27/2011, 3:06:49 PM
Andy
  • 49,085
  • 60
  • 166
  • 233
0

You could try

import os

info = os.popen('cmd /k systeminfo | find "Original Install Date"').read();
print info
#Original Install Date:     5/12/2014, 9:06:04 AM

I am yet to look into the subprocess package, but it is the recommended option. os will get the job done though.

GleasonK
  • 1,032
  • 8
  • 17
  • 1
    I would just read the output of `cmd /k systeminfo` and parse it in Python (loop through the lines and output the one which starts with 'Original Install Date'). You can obtain all the output of a process using `subprocess.check_output`. – nneonneo Jun 10 '14 at 15:01
0

Using the Windows registry:

import _winreg as reg
from datetime import datetime

key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
secs = reg.QueryValueEx(key, 'InstallDate')[0] # This is stored as a UNIX timestamp
date = datetime.fromtimestamp(secs)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • `FileNotFoundError:[WinError 2] The system can not find the file specified` –  Jun 10 '14 at 15:17
  • @begueradj: Check if that key exists in your registry. (I don't have an XP install, but this is said to work elsewhere). – nneonneo Jun 10 '14 at 15:19
  • The path you gave me is valid on my computer –  Jun 10 '14 at 15:22
  • 1
    I removed the 'r' just before `'SOFTWARE\Microsoft\...` you wrote, so it works now for me. I am going to use your solution because it runs more quickly and i can use the date in a variable for later needs in my Python program. Thank you for the help. Also, I am using Python-3.4.1. So I had to change _winreg to winreg as the official documenation asks me. –  Jun 10 '14 at 15:32
  • @begueradj: Ah. I wrote the answer assuming you were using Python 2.x. – nneonneo Jun 10 '14 at 15:39
  • Yes, I know, I just precised my comment for people who are using Python 3-4.1. Otherwise in Python 2.x no change to be done on your solution –  Jun 10 '14 at 15:41
0
def getInstallDate():
  cache = os.popen2("SYSTEMINFO")
  info = cache[1].read()
  label = "Original Install Date:" 
  return info.split(label)[-1].split("\n")[0].strip()
nneonneo
  • 171,345
  • 36
  • 312
  • 383
CCKx
  • 1,303
  • 10
  • 22
-1

Try:

import platform
print platform.system(), platform.release()
Sean Keane
  • 411
  • 1
  • 6
  • 19