4

I want to open up an exe I created, which is in the System32 folder of Windows. I do so through the command:

subprocess.call(["C:\\Windows\\System32\\ListTest.exe"])

But somehow Python does not find the System32 folder. I copied my exe over to the "System" directory in Windows, and if I want to open the exe there through Python, everything works fine. Why does Python not find the System32 directory?

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
user7353965
  • 75
  • 1
  • 8
  • try with subprocess.call(["C:/Windows/System32/ListTest.exe"]) – Harsha Biyani Jan 13 '17 at 08:31
  • same error...WindowsError: [Error 2] The system cannot find the file specified – user7353965 Jan 13 '17 at 08:34
  • Run with elevated privileges? – 101 Jan 13 '17 at 08:35
  • Ran pycharm as admin with same result :/ – user7353965 Jan 13 '17 at 08:42
  • 1
    are you using a 64 bit OS and a 32 bit Python 2 installation or something? http://www.samlogic.net/articles/32-64-bit-windows-folder-x86-syswow64.htm – Keith Hall Jan 13 '17 at 08:58
  • 1
    @KeithHall, you beat me to it. Almost certainly the OP is running 32-bit Python, so System32 is redirected to SysWOW64, which has the WOW64 system's 32-bit executables. In this case use the virtual SysNative directory to access the real 64-bit System32. – Eryk Sun Jan 13 '17 at 08:59
  • 1
    Also, don't use the System32 directory as your personal stomping ground. That's for operating system executables and DLLs. Install your program to its own directory and add it to `PATH`. – Eryk Sun Jan 13 '17 at 09:01
  • Right I am using a 64 bit OS and 32 bit python. Where do I find the Sysnativ dir? No worries, I want to call an exe which is already in the System32 folder of windows, it's a system exe not mine – user7353965 Jan 13 '17 at 09:03
  • 2
    SysNative is virtual. You won't find it in Explorer. In a 32-bit process, accessing `%SystemRoot%\SysNative` is automatically redirected to the real `%SystemRoot%\System32` folder. For example, `import platform;` `system32 = os.path.join(os.environ['SystemRoot'], 'SysNative' if platform.architecture()[0] == '32bit' else 'System32');` `listtest_path = os.path.join(system32, 'ListTest.exe')`. – Eryk Sun Jan 13 '17 at 09:10
  • thanks, now it works :) – user7353965 Jan 13 '17 at 09:37

3 Answers3

2

@eryksun and @Keith Hall had the right answer.

Since I am using a 64bit OS with a 32bit python it looks in the wrong directory.

system32 = os.path.join(os.environ['SystemRoot'], 'SysNative' if 
platform.architecture()[0] == '32bit' else 'System32')
listtest_path = os.path.join(system32, 'ListTest.exe')
subprocess.call([listtest_path])

is the full code now

user7353965
  • 75
  • 1
  • 8
  • Strictly it would be better to use `is_wow64 = (platform.architecture()[0] == '32bit' and 'ProgramFiles(x86)' in os.environ);` `system32 = os.path.join(os.environ['SystemRoot'], 'SysNative' if is_wow64 else 'System32')`. That should handle the rare case of Python running on natively 32-bit Windows. – Eryk Sun Jan 13 '17 at 10:05
0

Try with shell =True :

import subprocess
subprocess.call('dir', shell=True)
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
-1
is_wow64 = (platform.architecture()[0] == '32bit' and 'ProgramFiles(x86)' in os.environ); 
system32 = os.path.join(os.environ['SystemRoot'], 'SysNative' if is_wow64 else 'System32')