python怎么用记事本打开文件_如何使用tkfiledialog打开文件并使用记事本读取内容...

该博客讨论了在Python中使用tkinter模块打开文件对话框的问题。作者尝试通过filedialog.askopenfile打开文本文件,并希望在记事本中显示文件内容,但遇到了错误。他们分享了尝试将文件路径与记事本路径结合以通过subprocess.Popen打开文件的代码,但遇到了命令行错误。目前,代码尚无法成功在记事本中打开所选文件。
摘要由CSDN通过智能技术生成

我想用tkfiledialog打开所选文件的内容。当我选择文件并单击“打开”按钮时,文件不会打开,而是关闭对话框。如何使用记事本打开所选文件,以便能够读取文件中的内容。在from tkinter import *

from tkinter import filedialog

def my_file():

filename = filedialog.askopenfile(mode="r", initialdir="/", title="select file",

filetypes=(("text files", "*.txt"), ("all files", "*.*")))

root = Tk()

root.geometry("300x300")

#open the selected txt file with notepad to read the content

b = Button(root, text="open text file", command = my_file).pack()

root.mainloop()

编辑

在@PM 2RING和@Erik的提示下,我更改了文件对话框.askopenfile到文件对话框.askopenfilename把它放回去打开记事本.exe当我选择文件时。

代码如下:

^{pr2}$

它输出以下错误:Blockquote'C:/notepad.exet' is not recognized as an internal or external command,

operable program or batch file.

Blockquote

但是当我把返回改为打印时,它把目录打印到终端subprocess.Popen([r'C:\Program Files (x86)\Notepad.exe' + f])

它也不能用这个来打开。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值