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.
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)
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:
- run
regedit.exe
- go
hkey_classes_root
, pick desired extension, i.e..py
. read(default)
value on left pane - class name, i.e.python.file
. - open class name subtree in
hkey_classes_root
. should containshell
subtree, under find available shell actions. me , python scriptsedit idle
,edit pythonwin
. - pass these values second parameter of
shellexecute()
Comments
Post a Comment