python - How to check if multiple files exist in different directories -
i know how use python check see if file exists, after trying see if multiple files of same name exist throughout working directory. take instance:
gamedata/areas/ # have 2 folders in directory # testarea , homeplace 1. gamedata/areas/testarea/ 2. gamedata/areas/homeplace/
each folder of homeplace , testarea instance contains file called 'example'
is there pythonic way use 'os' or similiar check see if file 'example' can found in both testarea , homeplace?
although way without manually , statically using
os.path.isfile()
because throughout life of program new directories made, , don't want go code change it.
you can check in every directory bellow gamedata/areas/
: goes down 1 level, extend go down many levels want.
from os import listdir os.path import isdir, isfile, join base_path = "gamedata/areas/" files = listdir(base_path) only_directories = [path path in files if isdir(join(base_path,path))] directory_path in only_directories: dir_path = join(base_path, directory_path) file_path in listdir(dir_path): full_file_path = join(base_path, dir_path, file_path) is_file = isfile(full_file_path) is_example = "example" in file_path if is_file , is_example: print "found one!!"
hope helps!
Comments
Post a Comment