1

I want to check if a given file is within a specific directory and it's including subdirectories .

I used this code :

def Grep(givenfile,givendir):
    # iterate top down till in directory path
    for (root,dirs,files) in os.walk(givendir):
        if givenfile in files:
            # if file present return true
            return True
    return False

## Test.py my file that contain my code 
##C://Users//LENOVO//Desktop//python2021 this my path that contain my files 
Grep("Test.py",r"C://Users//LENOVO//Desktop//python2021")

When i run the code that is no output shown ?

Is there a better way to check if a given file is within a specific directory and it's including subdirectories

Thanks

saso1991
  • 31
  • 4

1 Answers1

0

There's no output because you're not printing anything. Try this as the last line:

print(Grep("Test.py",r"C://Users//LENOVO//Desktop//python2021"))
Cyphase
  • 11,502
  • 2
  • 31
  • 32