Question

I'm creating a Python 3 program for Windows 7 to search for common names for installation executables on a CD and run it. I have tried using multiple os.path.exists but when it finds the correct file, it prints that it can't find the other possible file names. Please help!

        if os.path.exists("D:/autorun.exe"):
            os.startfile("D:/autorun.exe")
        else:
            print("Failed Attempt!")
        if os.path.exists("D:/Install.exe"):
            os.startfile("D:/Install.exe")
        else:
            print("Failed Attempt!")
        if os.path.exists("D:/AutoRun.exe"):
            os.startfile("D:/AutoRun.exe")
        else:
            print("Failed Attempt!")
        if os.path.exists("D:/install.exe"):
            os.startfile("D:/install.exe")
        else:
            print("Failed Attempt!")
Was it helpful?

Solution

As you code appears to appears to be at the same indent level, each one of your if/else blocks will be executed one after another, therefore printing "Failed Attempt!" for each filepath that does not exist. Given your comment on March 12, you could use a for loop followed by a if/else block to ensure that "Failed Attempt!" is printed only once all files had been checked for existance. See the code below (NOTE: As I don't have Windows 7 handy, I have not tested this, but it should work):

import os
FILES = ('D:/autorun.exe', 'D:/Install.exe', 'D:/AutoRun.exe', 'D:/install.exe')
FILE_FOUND = False

for file in FILES:
    if os.path.exists(file):
        FILE_FOUND = file
        break

if FILE_FOUND:
    os.startfile(FILE_FOUND)
else:
    print("Failed Attempt!")

So to break it down step-by-step:

  1. Import the os module, which you have obviously done this elsewhere already.

  2. The filepaths for each file to be checked are to be stored as a tuple in FILES. A tuple is used because it is 'immutable' and therefore we know it will be stored exactly as shown above. This is also handy as it will mean that the for loop will check each file in the order from left to right.

  3. FILE_FOUND is initially assigned as False. This will be used later for 2 things:

    • To store the filepath string of a file from FILES, if it exists.
    • To be use to determine if os.startfile() should be executed.
  4. Next is the for loop. Each filepath string in the FILES tuple will be made available to be checked by the indented code following for file in FILES:. The file variable is a string representing the current filepath from FILES, and it is local to the code associated with the for loop. The loop then checks if a file exists at the given filepath string in file. If it the file exists, FILE_FOUND is assigned the string stored in file. The break statement then exits the for loop without any regard for any items in FILES that haven't been checked. Otherwise, the for loop will continue to run until it runs out of items to check in FILES.

  5. Finally, the if/else code block will only run once the for loop has either found a file that exists, or has finished working through all items in FILES. If FILE_FOUND evaluates as True (by virtue of not being None, 0, False, or as per the python docs), os.startfile(FILE_FOUND) will be executed, where we know that the filepath string of the existing file had been previously stored in FILE_FOUND by the for loop. If none of the filepath strings were found to exist in the for loop, the FILE_FOUND variable will still be False, therefore printing "Failed Attempt!".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top