How to open python file in default editor from Python script -


when try on windows webbrowser.open(fname) or os.startfile(fname) or os.system ('cmd /c "start %s"' % fname) python script getting executed.

how open edit in default editor (like sql script)

tried

import ctypes  shell32 = ctypes.windll.shell32 fname = r'c:\scripts\delete_records.py'  shell32.shellexecutea(0,"open",fname,0,0,5) 

it opens file explorer @ c:\program files\ibm\gsk8\lib64\c

default open , edit actions handled shellexecute winapi function (actions defined in registry in hkey_classes_root subtree).

there couple of way access winapi python script.

  1. using nicer wrapper pywin32. safer ctypes, non-standard python module. i.e:

    import win32api win32api.shellexecute(none, "edit", "c:\\public\\calc.bat", none, "c:\\public\\", 1) 
  2. using ctypes. trickier , doesn't control arguments, may cause fault (unless provide result type , arguments type manually).

    import ctypes shellexecutea = ctypes.windll.shell32.shellexecutea shellexecutea(none, "edit", "c:\\public\\calc.bat", none, "c:\\public\\", 1) 

to check actions supported desired filetype, following:

  1. run regedit.exe
  2. go hkey_classes_root , pick desired extension, i.e. .py. read (default) value on left pane - class name, i.e. python.file.
  3. open class name subtree in hkey_classes_root. should contain shell subtree, under find available shell actions. me , python scripts edit idle , edit pythonwin.
  4. pass these values second parameter of shellexecute()

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -