Python: 如何在记事本中打开json和html文件?

0 投票
1 回答
612 浏览
提问于 2025-04-17 16:57

我想在记事本中打开json和html文件。我有以下代码:

def openFile():
    fileName = listbox_1.get(ACTIVE)
    if fileName.endswith("json") or fileName.endswith("htm"):
        os.system("notepad.exe" + fileName)
    elif fileName.endswith("jpeg"):
        os.startfile(fileName)
    else:
        messagebox.showerror(title="Error", message="File is not relevant, please choose one with a directory")

现在的问题是,运行代码后只会出现一个命令提示符窗口,闪烁了一下就消失了。我希望文件的内容能够显示在记事本里。

我在Windows上使用的是Python 3.3

提前谢谢你们!

1 个回答

0

你在system调用中漏掉了一个空格。建议的修复方法是:

os.system('notepad.exe {}'.format(fileName))

撰写回答