Python: Try and Except usage to determine if a file path exists

44 Views Asked by At

I have a script that recursively searches source code for modules within that code to be able to source all required files.

I am working with a file structure that is somewhat predictable, so I can make a "best guess" to get the right path to this new module. There are some exceptions to this file path so it may not always lead to the right result.

I am attempting to use try catch to confirm that, for every module within a source file, there is actually a file at the path. If there isn't a file at the path, it makes another attempt of the path. If the module still doesn't exist, it will just remove the bad path from the modules array and skip it.

I want to print all of the correct paths to the files once the recursion is done and flag if there are file paths that aren't found.

My Python code is as follows:

modules = ["../pathsource1.c", "../path/source2.c"]

for module in modules: 
   try:
      with open(module, 'r') as f: 
          for line in f: 
              # search each line for keyword to see if there is a file
              if(module_name):
                 modules.append(path_to_module + module_name) 
   except: 
       print("path did not lead to a module.") 
       try:
           with open(new_module_path, 'r') as f:
               print("module found!")
               modules.append(new_module_path)
       except:
           print("module not found at any location")
       modules.remove(module) # remove old path, which was broken

print(modules) # should return only paths that exist  

expected output:

["../path/source1.c", "../path/source2.c", "../path/module1_in_source1.c", "path/module2_in_source1.c", "../path/module1_in_module2_in_source1.c", ...]

It works decently but it is not perfect. Sometimes the code is not raising an exception and then an incorrect path stays in the modules array. How do I improve this code?

1

There are 1 best solutions below

2
PedroRB On

You probably need to add a bit more explanation. Why are you waiting for an exception? I assume you expect a FileNotFoundError exception. It is a good practice to only catch the exception you want to handle. Otherwise, another exception could occur which isn't related to the FileNotFound.

You probably could use the os module os.path.exists() to check if a path exists.

Another problem causing your error could be that you are modifying your list at the same time you are looping through it. So when you remove an element from the list the indexes update so now your current element is now the next element. So in the next iteration of the for loop after an element removal, you will actually skip one because the list was updated. I suggest you to create a new list with the valid paths instead of removing from the one you are looping over.