Python opens external files in a number of ways,
Os.popen
Open an external program, but discover that only files in the directory where the files are located can be opened
Os.system
To open an external file
command to execute, equivalent to the command entered in the Windows CMD window. If you want to pass parameters to a program or script, you can use a space-delimited program and multiple parameters.
Os.startfile (recommended)
To open an external program
Os.startfile (path [, Operation])
This performs the same action as double-clicking the file in Windows Explorer. After the application is run, the function returns. You cannot also wait for completion or get the exit code from the application. The value of path is relative to the current directory. Operation is an optional string that specifies the action to be performed when the path is opened. Its default value is ' open ', but it can also be set to ' print ', ' edit ', ' explore ' or ' find ' [the exact list is related to the type of path (Windows)].
Win32process. CreateProcess
Parameter description:
CreateProcess (AppName, CommandLine, Processattributes, Threadattributes, bInheritHandles, dwCreationFlags, Newenvironment, CurrentDirectory, Startupinfo)
The parameters have the following meanings. AppName: The executable file name. CommandLine: command-line arguments. Processattributes: The Process security property, or the default security attribute if none. Threadattributes: The thread-safe property, or the default security attribute, if none. bInheritHandles: Inheritance flag. dwCreationFlags: Create a flag. Newenvironment: Creates environment variables for the process. CurrentDirectory: The current directory of the process. Startupinfo: Creates the properties of the process. |
Use:
1 Import win32process 2 win32process. CreateProcess ('c:\\windows\\notepad.exe', none, none, 0, win32process. Create_no_window, none, none, win32process. Startupinfo ())
----------------------
Recommended use of Startfile and win32process
1obj='C:\Users\Administrator\Desktop' or 'C:\Users\Administrator\Desktop\chrome.exe'2 ifObj andOs.path.exists (OBJ):#file or directory exists3 ifOs.path.isfile (OBJ):4 Importwin32process5 Try:#Open an external executable program6Win32process. CreateProcess (OBJ,"', none, none, 0, win32process. Create_no_window, none, none, win32process. Startupinfo ())7 exceptException, E:8 Print(e)9 Else:TenOs.startfile (str (OBJ))#Open Directory One A Else:#directories that do not exist - Print('directories that do not exist')
Python Open Directory with specified file