os.startfile and unknown file extension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dazzler
    New Member
    • Nov 2007
    • 75

    os.startfile and unknown file extension

    I need to open PDF file with my python application, and I'm using os.startfile(fi lename.pdf) command, I would need open command act 100% like clicking the file in the windows explorer

    python documentation about os.startfile: "Start a file with its associated application. This acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated."

    well this isn't true! what happens when you doubleclick file with unknown file extension in Windows Explorer? popup appears saying that the file extension is unknown! and this isn't happening when using os.startfile()

    does anyone have solution for this?

    os.startfile() returns error "1155", No application is associated with the specified file for this operation

    so should I check for this error message and then manually start that unknown file extension popup "applicatio n" ? not really a python question but does anyone know how to start that popup externally? =)
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    ok, I have almost solved the problem

    [code=python]
    try:
    os.startfile("t estfile.pdf")
    except Expection, errorcode:

    if errorcode[0] = 1155:
    os.popen("Rundl l32.exe SHELL32.dll, OpenAs_RunDLL testfile.pdf")
    else:
    print "other error"
    [/code]

    this works but as you know os.popen stops python code and not resume until I close it, I need to my code continue ofcourse and not stop for that popup =(

    how to use os.startfile() with "Rundll32.e xe SHELL32.dll, OpenAs_RunDLL testfile.pdf" command =/ it seems that os.startfile() take only filename and not any optional arguments

    you can yourself try in windows using start->run... and see how the "Rundll32.e xe SHELL32.dll, OpenAs_RunDLL testfile.pdf" acts :)

    Comment

    • dazzler
      New Member
      • Nov 2007
      • 75

      #3
      instead of
      [code=python]
      os.popen("Rundl l32.exe SHELL32.dll, OpenAs_RunDLL testfile.pdf")
      [/code]

      I tried
      [code=python]
      os.spawnl(os.P_ NOWAIT, os.environ['WINDIR']+"/system32/Rundll32.exe", "Rundll32.e xe SHELL32.DLL, OpenAs_RunDLL testfile.pdf")
      [/code]
      os.environ['WINDIR'] points to c:/windows or whatever the win installation directory is

      it isn't pretty but it works, don't know why I have to give the name "Rundll32.e xe" in the argument also, well it doesn't matter what there is ("a" works also) but if I dont give it, it won't work. maybe it's some linux thing =p

      if someone knows cleaner solution, tell me =)

      Comment

      Working...