如何从Windows下的命令行打开文件进行编辑?

sor*_*rin 5 python windows batch-file shellexecute

如何edit从Windows下的命令行打开文件?

主要是我希望在与之关联的默认编辑器中打开该文件(不要与此文件类型的默认操作混淆).

这不仅仅是"执行"文件,因此start filename不是解决方案.

注意:这需要以某种方式使用ShellExecute.

更新:我添加了Python作为替代batch.

sor*_*rin 2

下面是一个示例 Python 脚本,如果有分配给文件类型的编辑器,则该脚本将打开文件进行编辑。

import os
from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

filename = "readme.txt"
os.startfile(filename, "edit")

try:
    os.startfile(filename, "edit")
except WindowsError, e:
    MessageBox(text=str(e))
Run Code Online (Sandbox Code Playgroud)