0

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

1 Answers1

0

You could check if the file exists first using:

import os
path_exists = os.path.isdir("###your_dir###")
file_exists = os.path.exists("text.txt")
skymon
  • 850
  • 1
  • 12
  • 19
  • Hmm, I can clearly see that the text.txt is there in File Explorer, however the return value is False? Any reason for this – Hansa Jamak Aug 07 '19 at 08:38
  • Are you sure that the file is in the directory that your script accesses? What does `os.getcwd()` give you? – skymon Aug 07 '19 at 08:47
  • 'C:\\WINDOWS\\system32', and how would i allow python and/or CMD to acess the path entered – Hansa Jamak Aug 07 '19 at 08:47