如何为我自己的代码编辑器的不同文件夹的 python 文件创建函数?

How to create function to python file of different folder my own code editor?

我正在创建代码编辑器,但我的代码只是 运行 python 文件,该文件位于代码编辑器文件所在的同一文件夹中 当我在侧栏中打开另一个文件夹并从 select 中打开一个文件 运行 时,我的终端显示错误 我尝试了很多次,但我无法修复它 请告诉我如何修复它

错误:-

python: can't open file 'D:\coding notes\pytho project\Anmol.py': [Errno 2] No such file or directory

这是我的代码:-

import os
import subprocess
from tkinter import*
from tkinter import ttk
from tkinter.filedialog import askdirectory, asksaveasfilename
def process_directory(parent,path):
    for i in os.listdir(path):
        abspath = os.path.join(path,i)
        dirv = os.path.isdir(abspath)
        oid = tree.insert(parent,END,text=i,open=False)
        if dirv:
            process_directory(oid,abspath)
def Open(event=None):
    global path
    for i in tree.get_children():
        tree.delete(i)
    path = askdirectory()
    abspath = os.path.abspath(path)
    root_node = tree.insert("",END,text=abspath,open=True)
    process_directory(root_node,abspath)
def select_file(event=None):
    global file
    item = tree.selection()
    file = tree.item(item,"text")
    abspath = os.path.join(path,file)
    editor.delete(1.0,END)
    with open(abspath,"r") as f:
        editor.insert(1.0,f.read())
def save(event=None):
    global file
    if file == "":
        saveas()
    else:
        item = tree.selection()
        file = tree.item(item,"text")
        filepath = os.path.join(path,file)
        with open(file,"w") as f:
            f.write(editor.get(1.0,END))
        root.title(os.path.basename(file) + "-Python")
def saveas(event=None):
    global file
    file = asksaveasfilename(defaultextension=".py",filetypes=[("Python Files","*.py")])
    if file == "":
        file = None
    else:
        with open(file,"w") as f:
            f.write(editor.get(1.0,END))
        root.title(os.path.basename(file) + "-Python")
def run(event=None):
    global file
    if file == "":
        pass
    else:
        command = f"python {file}"
        run_file = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
        Output, error = run_file.communicate()
        output.insert(END,f"{file}>>\n")
        output.insert(END,Output)
        output.insert(END,error)
root = Tk()
tree = ttk.Treeview()
tree.pack(side=LEFT,fill=BOTH)
file = ""
path = ""
editor = Text()
editor.pack(expand=True,fill=BOTH)
output = Text(height=15)
output.pack(expand=True,fill=BOTH)

root.bind("<Control-Alt-o>",Open)
root.bind("<Control-s>",save)
root.bind("<Control-Alt-s>",saveas)
root.bind("<Shift-Return>",run)
tree.bind("<<TreeviewSelect>>",select_file)





root.mainloop()

如果所选文件位于所选文件夹的 sub-folder 内,则 select_file() 内由 abspath = os.path.join(path,file) 创建的 绝对路径 是不是正确的绝对路径(错过了 sub-folder 信息)。

其中一种方法是在插入树视图时在values选项中保存绝对路径:

def process_directory(parent,path):
    for i in os.listdir(path):
        abspath = os.path.join(path,i)
        dirv = os.path.isdir(abspath)
        # save the absolute path in "values" option
        oid = tree.insert(parent,END,text=i,open=False,values=(abspath,))
        if dirv:
            process_directory(oid,abspath)

然后在select_file()里面可以通过获取values选项获取绝对路径,而不是加入选中的文件夹和选中的文件:

def select_file(event=None):
    global file
    item = tree.selection()
    # get the absolute path
    file = tree.item(item,"values")[0]
    if os.path.isfile(file):
        editor.delete(1.0,END)
        with open(file,"r") as f:
            editor.insert(1.0,f.read())

同样适用于 save()