[Python-au] python library to discover file associations
raf at raf.org
Mon Feb 21 23:18:19 UTC 2011
Mike Dewhirst wrote:
> I want to launch the system text editor with a known text file but I
> don't know what that editor might be on which platform.
> Is there a Python library which can discover the system text editor on
> any platform?
if you just want to launch the text editor rather than
identify it, you could try something like the following.
if you are just interested in windows, os.startfile()
is what you are looking for (rather than os.system()).
def display_file(filename, action='open'):
'''Display the given filename in a separate application. On Windows
platforms, the action parameter may be 'open' or 'print' and is passed
to os.startfile() as an indication of what the separate application is
to do with the file. On other platforms, it is ignored.'''
cmd = none
import os, sys
if os.name == 'nt':
elif os.name == 'posix':
if sys.platform == 'darwin':
cmd = 'open'
elif filename.lower[-4:] in ['.csv', '.xls', '.doc']:
cmd = 'soffice'
elif filename.lower[-4:] == '.pdf':
cmd = 'evince'
cmd = 'gedit'
except Exception, e:
print('Failed to display %s: %s\n%s' % (filename, e, traceback.format_exc()))
More information about the python-au