views:

682

answers:

7

Does anyone know how I would go about detected what bit version Windows is under Python. I need to know this as a way of using the right folder for Program Files.

Many thanks

+13  A: 

I guess you should look in os.environ['PROGRAMFILES'] for the program files folder.

THC4k
+1 for solving the problem instead of answering the question - not always a good thing, but in this case it is.
BlueRaja - Danny Pflughoeft
This is the right solution, rather than hardcoding a directory. However, this will not lead to the 32-bit program files on 64-bit Windows, if that's what is needed.
Mike Graham
Yes, it will lead to the correct Program Files on Win64. Here are my values on Win7 64:ProgramFiles=C:\Program Files (x86)ProgramFiles(x86)=C:\Program Files (x86)ProgramW6432=C:\Program Files
Adal
A: 
>>> 2**63-1 == sys.maxint
True

For 64 bit, and

>>> 2**31-1 == sys.maxint
True

For 32 bit.

Thomas Ahle
-1: This doesn't work, as it depends on whether you're running a 32 or 64-bit version of *Python* (it could be either on a 64-bit system).
Scott Griffiths
This solution is fragile and wrong. For example, on a 64-bit machine with 32-bit Python, this will give the wrong answer. Also, the program files directory isn't set in stone.
Mike Graham
A: 

There should be a directory under Windows 64bit, a Folder called \Windows\WinSxS64 for 64 bit, under Windows 32bit, it's WinSxS.

Hope this helps, Best regards, Tom.

tommieb75
This is fragile and hackish. Python provides built-in ways to directly access information about the OS, and it doesn't depend on the Windows installation directory being in a normal place or having a normal name.
Mike Graham
+7  A: 

platform module -- Access to underlying platform’s identifying data

>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')

Can anyone running 64-bit Windows tell us what 32-bit python running on 64-bit Windows returns?

Joe Koberg
But I agree, use the environment variable to locate %PROGRAMFILES%
Joe Koberg
On 64-bit Windows it returns the same thing.
Adal
This won't work, because python chose to always return win32 for compatibility. This is also why there are only 'hackish' way to find out.
Thomas Ahle
+2  A: 

You should be using environment variables to access this. The program files directory is stored in the environment variable PROGRAMFILES on x86 Windows, the 32-bit program files is directory is stored in the PROGRAMFILES(X86) environment variable, these can be accessed by using os.environ('PROGRAMFILES').

Use sys.getwindowsversion() or the existence of PROGRAMFILES(X86) (if 'PROGRAMFILES(X86)' in os.environ) to determine what version of Windows you are using.

Mike Graham
+1  A: 

The subject lines asks about detecting 64 or 32bit OS, while the body talks about determining the location of ProgramFiles. The latter has a couple of workable answers here. I'd like to add another solution generalized to handle StartMenu, Desktop, etc. as well as ProgramFiles: http://stackoverflow.com/questions/2216173/how-to-get-path-of-start-menus-programs-directory

matt wilkie
A: 

Many of these proposed solutions, such as platform.architecture(), fail because their results depend on whether you are running 32-bit or 64-bit Python.

The only reliable method I have found is to check for the existence of os.environ['PROGRAMFILES(X86)'], which is unfortunately hackish.

gauss256