[Python-au] python library to discover file associations
raf
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?
>
> TIA
> Mike
hi mike,
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()).
cheers,
raf
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':
os.startfile(filename, action)
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'
else:
cmd = 'gedit'
try:
import subprocess
subprocess.call([cmd, filename])
except Exception, e:
import traceback
print('Failed to display %s: %s\n%s' % (filename, e, traceback.format_exc()))
More information about the python-au
mailing list