OS independent file associate ?

norseman norseman at hughes.net
Thu May 14 16:40:06 EDT 2009


Stef Mientki wrote:
> hello,
> 
> I would like to make my programs available under the "standard" OS's, 
> like Windows, Linux  (,Mac)
> One of the problems I encounter, is launching of files through their 
> file associates (probably a windows only terminology ;-)
> Now I can detect the OS, but only the main OS and not e.g. Ubuntu / 
> Gnome or whatever I've to detect.
> Through trial and error I found a working mechanism under Ubuntu, but as 
> I have to specify "gnome", I doubt this will work under other Linux 
> systems.
> 
> any good solutions available ?
> 
> thanks,
> Stef Mientki
> 
>    import subprocess
>    CHM = '../numpy.chm'
> 
>    # works under Ubuntu
>    subprocess.Popen( "gnome-open " + CHM , shell = True )
> 
>    # doesn't work under Ubuntu
>    # (the list should be converted to a number of arguments,
>    #but that doesn't seem to work under Ubuntu.
>    subprocess.Popen( [ "gnome-open" , CHM] ,shell = True )
> 
>    # works under Windows
>    subprocess.Popen( CHM , shell = True )
> 
>    # works better under windows
>    win32help.HtmlHelp ( Win32_Viewer,
>                         str(CHM),
>                         win32help.HH_DISPLAY_INDEX,
>                         str ( keyword ) )
> 
>  
================================

General algorithm  which I have used for years.
Convert to specific syntax of compiler/interpreter/whatever...


Get OS from an OS call       Python has  platform   for that
Get the version              Python has  uname      for that
Get the version              Linux has   uname -a   for that

Pick your OS you use most and put that test first
   if it fails, try 2nd
   it it fails, try 3rd
   .
   .
each try uses the files suited to OS attempted


FYI
import platform
help(platform)
   (read)

os_is= platform.system()
   test for os_is == which



platform.dist()   check the doc - not sure this one does anything useful

platform.uname()  check the doc - Test it on Ubuntu, may be useful



Steve



More information about the Python-list mailing list