5

According to the Python documentation, os.startfile takes two arguments: path and operation. Path is fairly well described and self-explanatory, but for operation, all that is said is:

When another operation [(not 'open')] is given, it must be a “command verb” that specifies what should be done with the file. Common verbs documented by Microsoft are 'print' and 'edit' (to be used on files) as well as 'explore' and 'find' (to be used on directories).

This implies that other command verbs exist. Are there any other available command verbs? If so, what are they and what do they do?

Specifically, I would like to know the command verb associated with the "Open file location" operation.

Ian
  • 5,704
  • 6
  • 40
  • 72
  • You can probably find the valid verbose in some Windows documentation document. FYI if you want to open the location of the file you can simply use `os.dirname` to get the name of the parent directory and open that one... – Giacomo Alzetta Dec 07 '18 at 15:12
  • @GiacomoAlzetta I did look but I couldn't find that doc. The behavior with that is actually slightly different -- "Open file location" automatically highlights the file in question, allowing it to be easily found and opened by just pressing "Enter". – Ian Dec 07 '18 at 18:30

1 Answers1

4

Since all startfile does is basically a call to ShellExecuteW from shell32, this is not really Python-specific.

Microsoft docs indicate that the operations (“verbs”) available in the ShellExecute family of functions depend on the exact system (registry). As per that page, “commonly available verbs” are:

  • edit — Launches an editor and opens the document for editing.
  • find — Initiates a search starting from the specified directory.
  • open — Launches an application. If this file is not an executable file, its associated application is launched.
  • print — Prints the document file.
  • properties — Displays the object's properties.
Norrius
  • 7,558
  • 5
  • 40
  • 49
  • 2
    It depends on the file type's associated ProgId in the registry, among other things. For example, for .py scripts the default is "Python.File", which defines the standard "open" command but also multiple "Edit with IDLE" commands that display in a submenu of a .py file's right-click context menu. Running these commands requires the relative subkey path, e.g. `os.startfile('test.py', r'editwithidle\shell\edit37')`. Note that registry paths only use backslash as a path separator, not forward slash. – Eryk Sun Dec 07 '18 at 17:15
  • 2
    A useful command defined for many file types is "runas", which is like "open" except it sends the request to the Application Information service to run the file elevated (i.e. run as administrator, with a UAC consent dialog). Python's installer doesn't define "runas" for "Python.File", but you can create it by copying the "open" command. Then you can use `os.startfile` to run a script elevated, as long as it doesn't require command-line arguments. If you need command-line arguments, then you'll have to use PyWin32 or ctypes to call `ShellExecute[Ex]` directly. – Eryk Sun Dec 07 '18 at 17:22