2010-08-07 4 views

답변

3

내가 PyGTK를 사용하여 구체적으로 알고 있지만하지 않습니다 xdg-open 그렇게 작동합니다 같은 것을 실행 파일의 기본 응용 프로그램을 엽니 다

import os 
os.system('xdg-open ./img.jpg') 

편집 : 나는 subprocess 모듈 등을 사용하는 것이 좋습니다 것 의견에. 나는 정확하게 그것을 사용하는 방법을 잘 모르겠다. 따라서 os.system을 예제에서 xdg-open으로 사용했다.

+0

위대한 작품, 감사합니다! – mooware

+1

'system'을 쓸모 없게 사용합니다. 이것은'subprocess.check_call ([ "xdg-open", filename])'이어야합니다. – Philipp

+0

안녕하세요, 필립, '시스템'사용에 문제가 있습니까? – mooware

3

GNU/Linux에서 xdg-open을, Mac에서 open을 사용하고, Windows에서 start을 사용하십시오. 또한 subprocess을 사용하십시오. 그렇지 않으면 외부 앱을 호출 할 때 애플리케이션을 차단할 위험이 있습니다.

이 내 구현, 그것은 도움이되기를 바랍니다 : http://goo.gl/xebnV

import sys 
import subprocess 
import webbrowser 

def default_open(something_to_open): 
    """ 
    Open given file with default user program. 
    """ 
    # Check if URL 
    if something_to_open.startswith('http') or something_to_open.endswith('.html'): 
     webbrowser.open(something_to_open) 
     return 0 

    ret_code = 0 

    if sys.platform.startswith('linux'): 
     ret_code = subprocess.call(['xdg-open', something_to_open]) 

    elif sys.platform.startswith('darwin'): 
     ret_code = subprocess.call(['open', something_to_open]) 

    elif sys.platform.startswith('win'): 
     ret_code = subprocess.call(['start', something_to_open], shell=True) 

    return ret_code 
관련 문제