AD WAN
AD WAN

Reputation: 1474

How to open file with tkfiledialog and read the content with notepad

I want to read the content of the selected file by opening with tk filedialog. When i select the file and click on open button the file doesn't open but rather close the dialog box .How can i open the selected file with notepad so i can be able to read the content in the file.

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()

EDIT With hint from @PM 2RING and @Erik i changed the filedialog.askopenfile to filedialog.askopenfilename to return it to open with notepad.exe when i select the file. THis is the code:

from tkinter import *
from tkinter import filedialog
import os


def my_file():
    filename = filedialog.askopenfilename( initialdir="C:/", title="select 
file", filetypes=(("text files", "*.txt"), ("all files", "*.*")))
    for f in filename:
        return f
    os.system(r"C:/notepad.exe" + f)


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()

it output this error :

Blockquote'C:/notepad.exet' is not recognized as an internal or external command, operable program or batch file. Blockquote

but when i changed the return to print it print the directory to terminal.I tried to open with subprocess

 subprocess.Popen([r'C:\Program Files (x86)\Notepad.exe' + f])

it also doesn't open with this one to.

Upvotes: 0

Views: 7366

Answers (3)

Ethan Field
Ethan Field

Reputation: 4730

There's a few things which need to be amended here.

First of all, C:/notepad.exe is not the location of Notepad (At least not on any Windows machine with a default setup), you can simply use notepad.exe which should make it more compatible with systems that have moved Notepad to another location (citation needed).

Secondly, executing . . .

for f in filename:
    return f
os.system(r"C:/notepad.exe" + f)

Doesn't do what you seem to think it does. What's actually happening here is that your program is loading the string into the loop, evaluating the first character (Probably "C") and then returning the string to the Button widget which doesn't receive any returned values. This then breaks your function, so it never actually reaches your declaration of os.system(r"C:/notepad.exe" + f).

You also need to include a space between the statement used to open Notepad notepad.exe and the actual file declaration f, otherwise you're running something like notepad.exeC:/text.txt which is going to throw an error at you.

What you should be doing is something like the below:

from tkinter import *
from tkinter import filedialog
import os

def my_file():
    filename = filedialog.askopenfilename( initialdir="C:/", title="select file", filetypes=(("text files", "*.txt"), ("all files", "*.*")))
    os.system(r"notepad.exe " + filename)

root = Tk()
root.geometry("300x300")
b = Button(root, text="open text file", command = my_file).pack()

root.mainloop()

I'd like to add that I have no clue why you're doing this, why not simply display the text in a Text widget? What you're doing here is adding one extra step for people to open files in Notepad, instead of opening file explorer, finding the file and then opening it they have to open your program, then open file explorer, then find the file and then open it. It's adding at least one extra click not to mention the load time of your program.

Upvotes: 3

Erik
Erik

Reputation: 1226

As mentioned by PM 2Ring, I would use the os.system function. As mentioned in its description, "os.system(command)" let's you execute the command as if you had written it in the command prompt, so os.system("Notepad c:/users/yourName/junk.txt) would open a file named junk.txt if it were at that location.

In other words, once you have the filename from your filedialog.askopenfilename call, do something like this:

import os
os.system("Notepad " + filename) # where filename is the string that filedialog.askopenfilename returns

The implementation into your code shouldn't be too bad, but if you need a more complete example, let let me know.

Upvotes: 1

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

The following code will display a button and a text widget. The button will load in your file, and the text widget will display it. I assume this is what you had in mind?

from tkinter import *
from tkinter import filedialog

def my_file():
    file = filedialog.askopenfile(mode="r", initialdir="/", title="select file",
                                   filetypes=(("text files", "*.txt"), ("all files", "*.*")))
    t.insert(END, file.read())
    file.close()

root = Tk()
root.geometry("300x300")
                          #open the selected txt file with notepad to read the content
t = Text(root)
t.grid(row=0, column=0)
b = Button(root, text="open text file", command = my_file)
b.grid(row=1, column=0)

root.mainloop()

Upvotes: 0

Related Questions