-1

I am working with the subprocess.run function in Python to open up file explorer in Windows and search/open a specific folder. I found online that the correct code line is:

subprocess.run(['explorer', folder])

My question is, how did they know 'explorer' was the right argument to put in there? I cannot find any reasoning behind it anywhere.

tripleee
  • 175,061
  • 34
  • 275
  • 318
R.Lee
  • 13
  • 2
  • 3
    It's `explorer.exe`, located in `C:\Windows\explorer.exe`. Go to `cmd`, type in `explorer`, and see what happens. – iz_ Feb 15 '19 at 03:03
  • You're asking why Windows acts a certain way, or how to know how Windows works? Both of those questions are just asking for trouble. I'd say just be happy that SO, or whatever, gave you the answer you were looking for. -- or Timothy32's answer...that works too ;) – CryptoFool Feb 15 '19 at 03:03
  • Welcome to Stackoverflow. You may find that searching with a search engine (or the builtin search here) using a variety of search terms that describe your problem will bring up helpful threads on this, such as [this one on finding executable on Windows](https://stackoverflow.com/questions/4965175/make-subprocess-find-git-executable-on-windows) or [the role the `PATH` environment](https://stackoverflow.com/questions/5658622/python-subprocess-popen-environment-path) has on this. – metatoaster Feb 15 '19 at 03:04

1 Answers1

1

The first item in the argument list is the name of the executable to run. It's what you would type if you opened command prompt to run file explorer. For example you might run the following in command prompt:

explorer C://Users/

The command prompt splits up what you type into a list, where the first argument is the process to run (explorer) and the rest are the arguments to send to that process (['C://Users/']). When you use subprocess.run in python, it doesn't automatically split up what you enter, so you need to give it a list of arguments directly.

Whoever wrote that code you found knew that explorer was the name of the process to run to launch the file explorer. Sometimes finding the command to run to launch a certain process can be tricky in windows - task manager might be able to give that information if you find a process that's already running, I'm not sure.