Python Open Directory with specified file

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.