0

HELO

I am new to Python and learning a lot thanks to Stackoverflow, but at the moment I am stuck at this simple task where I was sure I would resolve it myself but spent few hours looking around.

I have Tkinter GUI with a button, where if pressed it will look for such name in specific file path and if it finds "Check For Updates", it will run it else it will do nothing as expected.

import tkinter as tk
import os

root = tk.Tk()

button = tk.Button(root, text="Open", height=1, width=25, borderwidth="2", command=lambda: openupdt())
button.grid()

def openupdt():
    os.startfile(r"C://ProgramData//Microsoft//Windows//Start Menu//Programs//Java//Check For Updates")

root.mainloop()

Here I tried to use IF statement but it seems like I am doing something wrong. If no file in such path is found I would like it to print message or do what ever is instructed.

def openupdt():
    os.startfile(r"C://ProgramData//Microsoft//Windows//Start Menu//Programs//Java//Check For Updates")
    if openupdt == False:
        print("No such file")

gives me an error "The system cannot find the file specified:" as if it fully ignores IF statement

Thank you.

2
  • The condition if openupdt == False: inside of the definition of a function named openupdt doesn't make sense. What is that supposed to do? In any event, you can use try ... except to catch errors which occur when you try to open a file. Commented Dec 10, 2022 at 13:59
  • button = tk.Button(root, text="Open", height=1, width=25, borderwidth="2", command=lambda: foo() if os.path.exists(file_path) print("No such file")) Commented Dec 14, 2022 at 17:17

3 Answers 3

2

If you want to check, if something with certain path exists or not, use os.path.exists that returns true or false. In your case, it might be like

import os

def openupdt():
    if os.path.exists("/your/path"):
        do_something()
    else:
        print("No such file or directory")

The main problem in your code is fact that in if statement you compare False with openupdt, but your function name is openupdt. You are actually comparing function to bool value, that's incorrect. Better make use of os.path.exists like I provided above and this will work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Very much appreciated for your feedback! The if os.path.exists("/your/path"): did the trick! I guess overthinking sometimes leads to not seeing things.
0

You should first check if the file exists and then use the os.startfile.

Try something like:

if os.path.isfile("your_path_to_file"):
    os.startfile("your_path_to_file")
else:
   print("No such file")

You can also use try...except and handle the error as it appears:

try:
    os.startfile("your_path_to_file")
except FileNotFoundError:
    print("No such file")
    

Comments

0

The main thing is that you should be using a try-except for os.startfile(). Additionally your if statement is checking the function instead of a variable which makes the comparison not viable. Plus you are using a raw string literal so you don't need the extra slashes.

def openupdt():
    try:
        os.startfile(r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Java\Check For Updates")
    except:
        print("No such file")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.