我正在創建一個代碼編輯器,但我的代碼僅運行 python 檔案,該檔案位于代碼編輯器檔案也存在的同一檔案夾中,當我在側欄中打開另一個檔案夾并從中選擇一個檔案并運行它時,我的終端顯示錯誤我試過很多次,但我無法修復它 請告訴我如何修復它
錯誤:-
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()
uj5u.com熱心網友回復:
如果選中的檔案在選中檔案夾的子檔案夾內,那么inside創建的絕對路徑不是正確的絕對路徑(錯過子檔案夾資訊)。abspath = os.path.join(path,file)select_file()
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()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446464.html
上一篇:如何在Tkinter的不同框架內實際添加按鈕和其他內容
下一篇:在第二個視窗中保存用戶輸入
