python調用exe程序

起初使用os.system(),使用cmd打開程序,但是會顯示cmd窗口影響程序的美觀;查找python文檔發現os.startfile()方法,文檔如下:

Start a file with its associated application.

When operation is not specified or 'open', this acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to thestart command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.

When another operation 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).

startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status. The path parameter is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash ('/'); the underlying Win32ShellExecute() function doesn’t work if it is. Use theos.path.normpath() function to ensure that the path is properly encoded for Win32.

Availability: Windows.

但是os.startfile()只能是像雙擊文件那樣打開程序,不能再打開的同時傳遞參數,os.popen()可以既沒有cmd窗口,同時接收傳遞的參數結合了兩者的需求。詳細用法見文檔:

Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as the corresponding argument to the built-in open() function. The exit status of the command (encoded in the format specified for wait()) is available as the return value of the close() method of the file object, except that when the exit status is zero (termination without errors), None is returned.







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章