Search code examples
pythonpython-2.7subprocesswindows-explorererror-code

Why does opening an explorer window and selecting a file through Python's subprocess return error code 1


I'm writing some code that will open an explorer window and select the file in Windows for a given file path.

I found a solution here, which on the face of it seems to work. It opens Windows Explorer and highlights the file as desired. However, I get an error code of 1 returned. Ideally, I'd like to know if the action was successful, by checking the return code, but I'm getting 1 even when it appears to be behaving correctly.

My code looks like this:

a_file = r"C:\a_path\to\afile.file"
cmd_args = ['explorer', '/select,', a_file]
p = subprocess.call(cmd_args)
print(p)

Which returns:

1

I have also tried using os.system and subprocess.Popen just in case that made a difference (which it didn't). I'm using Python 2.7, and testing on Windows 10 64bit though I would prefer my solution to be compatible with as many Windows and Python setups as possible.

If anyone knows why or how to avoid getting this, that would be amazing! Thanks


Solution

  • People have been observing Windows GUIs to return exit code 1 for a while, and come to accepting it.

    You might want to try to control your windows program through win32com, code snippet for explorer.exe here and comments here.