首页 > Python 如何让系统自动调用默认程序打开文件?

Python 如何让系统自动调用默认程序打开文件?

python 脚本生成 xls 文档后,希望自动调用系统的默认程序(比如 excel)打开该文档,应该如何操作?


windows上可以使用os.startfile

os.startfile(file)

linux上可以使用xdg-open

subprocess.call(["xdg-open", file])

mac os上可以使用open

subprocess.call(["open", file])

用标准库里的webbrowser试试?

import webbrowser
webbrowser.open('file:///D:/path/to/table.xls')

Python 调用 Windows API:
http://my.oschina.net/fants/blog/89682

使用 Windows API 启动程序(传个参数就可以打开文件啦):
http://www.cnblogs.com/greatverve/archive/2010/12/24/window-api-exe.html

【热门文章】
【热门文章】