各个操作系统下(MacOSX, Linux, X11, Windows),在python中实现双击打开程序的效果。类似os.startfile()

1.在python中好像有mailcap这个模块。
2.http://pypi.python.org/pypi/desktop这里还有一个专门统一桌面操作的python库
3.还有这样也行
os.spawnvp(os.P_WAIT, "run-mailcap", ["run-mailcap", name])
4.好像这样也行
import  os
if os.name == 'posix':
    def startfile(name):
        os.system('run-mailcap "%s"' % name)
    os.startfile = startfile
不过只是针对debian系列的linux
5.
http://groups.google.com/group/python-cn/browse_thread/thread/becb84da60efdc91

import os
if os.name=='posix':
    #cmd = "xdg-open '%s' &"
    cmd = os.popen('''which op xdg-open exo-open gnome-open kfmclient
open | head -n1 |  sed "s/$/ '%s' /&/; s/kfmclient/kfmclient exec/"
''').read()
elif os.name=='nt':
    cmd = 'start %s'

def opendoc(filename):
    '用操作系统默认程序打开文件'
    os.popen(cmd%filename)

这样就覆盖了从 Ubuntu、MAC OS X、XP、Vista 到 OpenSolaris 桌面操作系统了


#5 is the best solution for launching program associated with file.
Test code:

#!/usr/bin/env python
#coding = utf-8
import os, sys
cmd = None
if os.name=='posix':
    #cmd = "xdg-open '%s' &"
    cmd = os.popen('''which op xdg-open exo-open gnome-open kfmclient open | head -n1 |  sed "s/$/ '%s' /&/; s/kfmclient/kfmclient exec/"''').read()
elif os.name=='nt':
    cmd = 'start %s'

def opendoc(filename):
    os.popen(cmd%filename)

opendoc(sys.argv[1])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值