I'm trying to add a function that allows me to open a text file on a notepad built in python but this error shows up TypeError: expected str, bytes or os.PathLike object, not list
I'm actually really new to programming and i'm following this tutorial about how to make a notepad on python, i've tried importing os but i had no idea how to use it. Thanks in advance
from tkinter import Tk, scrolledtext, Menu, filedialog, END, messagebox
from tkinter.scrolledtext import ScrolledText
from tkinter import*
#Root main window
root = Tk(className=" Text Editor")
textarea = ScrolledText(root, width=80, height=100)
textarea.pack()
# Menu options
menu = Menu(root)
root.config(menu=menu)
filename = Menu(menu)
edicion = Menu(menu)
# Funciones File
def open_file ():
file = filedialog.askopenfiles(parent=root, mode='r+', title="Select a file")
if file == None:
contenidos = file.read()
textarea.insert('1.0', contenidos)
file.close
else:
root.title(" - Notepad")
textarea.delete(1.0,END)
file = open(file,"r+")
textarea.insert(1.0,file.read)
file.close()
def savefile ():
file = filedialog.asksaveasfile(mode='w')
if file!= None:
data = textarea.get('1.0',END+'-1c')
file.write(data)
file.close()
def exit():
if messagebox.askyesno ("Exit", "Seguro?"):
root.destroy()
def nuevo():
if messagebox.askyesno("Nuevo","Seguro?"):
file= root.title("Vistima")
file = None
textarea.delete(1.0,END)
#Funciones editar
def copiar():
textarea.event_generate("<<Copy>>")
def cortar():
textarea.event_generate("<<Cut>>")
def pegar():
textarea.event_generate("<<Paste>>")
#Menu
menu.add_cascade(label="File", menu=filename)
filename.add_command(label="New", command = nuevo)
filename.add_command(label="Open", command= open_file)
filename.add_command(label="Save", command=savefile)
filename.add_separator()
filename.add_command(label="Exit", command=exit)
menu.add_cascade(label="Editar", menu=edicion)
edicion.add_command(label="Cortar", command=cortar)
edicion.add_command(label="Pegar", command=pegar)
edicion.add_command(label="Copiar", command=copiar)
textarea.pack()
#Keep running
root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\57314\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/57314/PycharmProjects/text_editor/bucky.py", line 28, in open_file
file = open(file,"r+")
TypeError: expected str, bytes or os.PathLike object, not list
You are getting this error: TypeError: expected str, bytes or os.PathLike object, not list
This suggests that this line:
file = open(file,"r+")
is trying to open a list. Why might that be? Well, the function you are using here to assign the file
variable is returning a list of files not a single filename:
file = filedialog.askopenfiles(parent=root, mode='r+', title="Select a file")
Is there a chance you misread the tutorial and you should have written:
file = filedialog.askopenfilename(parent=root, mode='r+', title="Select a file")
Check out the subtle difference between the two functions here: http://epydoc.sourceforge.net/stdlib/tkFileDialog-module.html#askopenfiles.