I am currently working on a script that allows you to type in a path of a python script and run it, this is useful for running the same type of code a lot for troubleshooting because sometimes the program crashes CMD and you have to re-enter all the information of the location ECT.
At the moment it works perfectly, however it comes up with the error
Traceback (most recent call last):
File "E:\Coding Type of stuff\Python\Testing\test.py", line 1, in <module>
print(open("text.txt","r").readlines()[0])
FileNotFoundError: [Errno 2] No such file or directory: 'text.txt'
I am aware of why this is happening, but is there any fix?
Current code of python script runner:
import tkinter as tk
from subprocess import Popen
try:
path = (open("path.txt","r").readlines())[0]
print(path)
except:
path = "Enter path..."
open("path.txt","w").write("")
def on_entry_click(event):
if entry.get() == path:
entry.delete(0, "end")
entry.insert(0, '')
entry.config(fg = 'black')
def on_focusout(event):
if entry.get() == '':
entry.insert(0, path)
entry.config(fg = 'grey')
def get_input(box):
try:
path = box.get()
if path != "Enter path...":
open("path.txt","w").write(path)
Popen('python "'+path+'"')
else:
print("No path entered")
except:
print("That path does not work")
root = tk.Tk()
root.geometry("375x50")
label = tk.Label(root, text="Path: ")
label.grid(row = 0, column = 0)
entry = tk.Entry(root, bd=1, width = 50)
entry.insert(0, path)
entry.bind('<FocusIn>', on_entry_click)
entry.bind('<FocusOut>', on_focusout)
entry.config(fg = 'grey')
button = tk.Button(root, text = "Run", command = lambda: get_input(entry))
entry.grid(row = 0, column = 1)
button.grid(row = 1)
root.mainloop()