如何使用Python 打开exel等各种文件或者应用,就用:os.startfile

来源:互联网 发布:js unselectable 编辑:程序博客网 时间:2024/06/07 17:56

python虽然可以有库打开excel文件,但这个问题不是讨论这个。

而是如何通过python 直接打开各种各样的文件或者应用。

 

有几种方式,os.system,subprocess.Popen,os.startfile.

先看看各个介绍:

os.system:

system(...)
    system(command) -> exit_status

    Execute the command (a string) in a subshell.

subprocess.Popen:

class Popen(__builtin__.object)
 |  Methods defined here:
 |
 |  __del__(self, _maxint=2147483647, _active=[])
 |
 |  __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, st
derr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, un
iversal_newlines=False, startupinfo=None, creationflags=0)
 |      Create new Popen instance.

os.startfile:

startfile(...)
    startfile(filepath [, operation]) - Start a file with its associated
    application.

    When "operation" is not specified or "open", this acts like
    double-clicking the file in Explorer, or giving the file name as an
    argument to the DOS "start" command: the file is opened with whatever
    application (if any) its extension is associated.
    When another "operation" is given, it specifies what should be done with
    the file.  A typical operation is "print".

    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 filepath 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 Win32 ShellExecute function doesn't work if it is.

 

看到这块,已经很明确了,就是她了:os.startfile

如果只有路径,相当于在浏览器双击这个文件!

0 0
原创粉丝点击