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

Time:03-21

I am creating an code editor but my code is only run python file which is in same folder where code editor file is also present and when I open another folder in side bar and select a file from and run it than my terminal shows error I tried many times but I am unable to fix it Please tell me how to fix it

error:-

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

This is my code :-

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

CodePudding user response:

If the selected file is inside a sub-folder of the selected folder, then the absolute path created by abspath = os.path.join(path,file) inside select_file() is not the correct absolute path (miss the sub-folder information).

One of the way is to save the absolute path in values option when inserting into the treeview:

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)

Then inside select_file() you can get the absolute path by getting the values option instead of joining the selected folder and the selected file:

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

Same apply to save() as well.

  • Related