0
votes

I've made a script (called isPA64.bat) to determine if the executing system is 64-bit (based on this Bear's Log tip):

@echo off
setlocal
set str1=%PROCESSOR_ARCHITECTURE%
set/A sixty4=0
if not x%str1:64=%==x%str1% set/A sixty4=1
endlocal & exit/B %sixty4%

It gets called from another simple batch, named callpa.bat (it could be called directly, too, but this proves that ERRORLEVEL does, indeed, get set appropriately):

@echo off
ver>nul & (call isPA64.bat & if ERRORLEVEL 1 (echo 64-bit & exit/B 1) else (echo not 64-bit & exit/B 0))

Up to this point, this all works fine; however, I must call one of these two from a Python 3.7.2 program. I do this:

import subprocess
print(subprocess.run(["callpa.bat"]))

Simple enough, right? But I haven't been able to figure out how to get a valid return code back, in the python code... Is there a way to assign a variable in the python code to either the "exit"/return code or to the value of ERRORLEVEL, from the cmd.exe shell which executes the outer-level script? ...I can't find a way in the python doc's to do that.

1
Are you saying that print(subprocess.run(["callpa.bat"])) doesn't show you the right return code, or that it does and you just don't know how to programmatically get at it? - Joseph Sible-Reinstate Monica
Why not have just one batch file, callpa.bat, there's no need for isPA64: @If %PROCESSOR_ARCHITECTURE:~-2% Equ 86 (If Defined PROCESSOR_ARCHITEW6432 (Exit /B 0)Else Exit /B 1)Else Exit /B 0. You'll also note that I've used more robust code too, as a 64-bit system running under a 32-bit process will incorrectly return x86 without the additional check. - Compo
You might even find that disposing of both batch files and doing a similar thing by directly reading the environment variables, after importing os and reading os.environ['PROCESSOR_ARCHITECTURE'] and os.environ['PROCESSOR_ARCHITEW6432'] works for you. - Compo
Why not trying to detect the "bitness" directly in Python? Refer to Detect 64bit OS (windows) in Python as well as How to detect whether the OS supports 16-bit exes in Python? - aschipfl
Tried rc=subprocess.call("callpa.bat", shell=True)--tried Shell=False, too--but I always get a 0 ('echo %ERRORLEVEL%') on 64-bit machine (running 32-bit python--but w/ my code, that shouldn't matter); I should get a 1, as that is also what "callpa.bat" gives, when run directly from same Command Prompt window as Python app. I do plan to use a single Batch file, eventually--or even a better solution--however, the outer-level app is co-maintained in Batch and Python, so am hoping to re-use isPA64.bat (or an improved .bat, like Compo's)....want to keep 2 versions of app similar, for ease. - Rob F.

1 Answers

0
votes

No need of creating other batch files:

import os
arch = os.environ['PROCESSOR_ARCHITECTURE']
if arch == 'x86':
    print("x86")
    exit(0)
else:
    print("x64")
    exit(1)

Then import it be errorlevel variable.

WMI can be also used:

import sys
import win32com.client
wmiservice = win32com.client.dispatch("WbemScripting.SWbemLocator")
swbemservice = wmiservice.ConnectServer(., "root/cimv2")
items = swbemservice.ExecQuery("Select * From Win32_OperatingSystem")
for item in items:
    if item.OSArchitecture == 'x86':
        print("x86")
        exit(0)
    else:
        print("x64")
        exit(1)

You need to download python for windows extensions from http://sourceforge.net/projects/pywin32/files/