0

I'm sorry about the unclear title, I don't really know how to put it, but I am trying to make a text editor in python. I want to get the filename of a file the user open or saves in python:

def openFile():
    file = filedialog.askopenfile(parent=main_window, mode='rb', title='Select a file')
    contents = file.read()
    textArea.delete('1.0', END)
    textArea.insert('1.0', contents)
    file.close()

Here the user gets an dialog to select the file he wants to open. But how can I get the filename of the file he selects?

def saveFileas():
    file = filedialog.asksaveasfile(mode='w')
    data = textArea.get('1.0', END+'-1c')
    file.write(data)
    file.close()

Here the user gets a dialog to save his file, and input the name they want. Again, I need the name the user inputs. Both are using the default windows open and save dialog.

Here is all my code:

from tkinter import Tk, scrolledtext, Menu, filedialog, END

main_window = Tk(className = " Text Editor")
textArea = scrolledtext.ScrolledText(main_window, width=100, height=80)

# def newFile():
def openFile():
    file = filedialog.askopenfile(parent=main_window, mode='rb', title='Select a file')
    contents = file.read()
    textArea.delete('1.0', END)
    textArea.insert('1.0', contents)
    file.close()


def saveFileas():
    file = filedialog.asksaveasfile(mode='w')
    data = textArea.get('1.0', END+'-1c')
    file.write(data)
    file.close()

def saveFile():
    content = textArea.get('1.0', END+'-1c')
    if content:
        print("There is content")
    else:
        print("There is no content")


#Menu options
menu = Menu(main_window)
main_window.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="New")
fileMenu.add_command(label="Open", command=openFile)
fileMenu.add_command(label="Save As", command=saveFileas)
fileMenu.add_command(label="Save", command=saveFile)
fileMenu.add_separator()
fileMenu.add_command(label="Print")
fileMenu.add_separator()
fileMenu.add_command(label="Exit")

textArea.pack()

main_window.mainloop()
Conner Dassen
  • 729
  • 4
  • 11
  • 28

3 Answers3

1

Something like this should work for getting the filename.

  from  tkinter import filedialog
  root = Tk()
  root.filename =  filedialog.askopenfilename(initialdir = "Path Where the dialog should open first",title = 
  "Title of the dialog",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
  print (root.filename)
  root.withdraw()
0

The documentation seems to imply that askopenfile returns the filename, if one were selected, or an empty string, if the user clicked cancel. In other words, it's a string, not a file pointer. You'd need to open the file first.

sizzzzlerz
  • 4,277
  • 3
  • 27
  • 35
-1

askopenfile returns a handle to the file object like Brian Oakley said you can get the file name like so:

filePath = filedialog.askopenfile(parent=main_window, mode='rb', title='Select a file')
fileName = filePath.name.split('/').pop()
Iseis
  • 11
  • 1
  • 6
  • `askopenfile` returns a handle to a file object, not a path. Your code will throw an error. – Bryan Oakley Jun 02 '17 at 12:34
  • Oh wait never mind I was using askopenfilename my mistake. – Iseis Jun 02 '17 at 18:21
  • Updated my answer to use askopenfile instead of askopenfilename – Iseis Jun 02 '17 at 18:27
  • You've got that backwards. `askopenfilename` (with the *name*) returns a filename. `askopenfile` (without *name*) returns an open file handle. Your code example is still showing `askopenfile`. – Bryan Oakley Jun 02 '17 at 19:34
  • Yeah know it does, you can use the file handle returned by `askopenfile` to get the name by doing this `fileHandle.name` – Iseis Jun 05 '17 at 15:31