如何在 python 制作的记事本上打开文本文件?

How can i open a text file on a Notepad made in python?

我正在尝试添加一个允许我在 python 内置的记事本上打开文本文件的功能,但此错误显示为 TypeError: expected str, bytes or os.PathLike object,未列出

我实际上是编程新手,我正在学习如何在 python 上制作记事本的教程,我尝试导入 os 但我不知道如何导入用它。提前致谢

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:\Users314\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

该错误提示您正在发生的事情。

您收到此错误:TypeError: expected str, bytes or os.PathLike object, not list

这表明这一行:

file = open(file,"r+")

正在尝试打开列表。为什么会这样?那么,您在此处用于分配 file 变量的函数是 返回文件列表 而不是单个文件名:

file = filedialog.askopenfiles(parent=root, mode='r+', title="Select a file")

您是否有可能误读了教程,您应该这样写:

file = filedialog.askopenfilename(parent=root, mode='r+', title="Select a file")

在这里查看两个函数之间的细微差别:http://epydoc.sourceforge.net/stdlib/tkFileDialog-module.html#askopenfiles

我制作了一个没有 GUI 的类似应用程序,但它的核心是相同的。我没有在 tkinter 中输入文本,而是在控制台中使用了标准输入功能。这是我的可读代码:

(看我的阅读功能)

print('My NoteBook')
print('Note: Do not reuse file names for text will be overwritten.')
import sys
import replit 
exit=0
while exit!='y':
  m=input('Select an option: a)read your docs or b)write a doc or c)delete a doc ')
  def writes(): 
    title=input('Enter the title of this paper: ')
    textstuff = input('Write something: ')
    text_file = open(title,'w')
    text_file.write(textstuff)
    text_file.close()
  def read():
    inputFileName = input("Enter name of paper: ")
    inputFile = open(inputFileName, "r")
    for line in inputFile:
      sys.stdout.write(line)
  def delete():
    import os

    print("Enter the Name of Paper: ")
    filename = input()
  
    os.remove(filename)
    print("\nPaper deleted successfully!")
  
    

    
  


  if m=='a':
    read()
  elif m=="b":
    writes()
  else:
    delete()

  
  exit=input('\nDo you want to exit, y/n ')
replit.clear() 
sys.exit('Thank you' )