起初使用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.