Regarding question1:
You're asking about a glob expression. Note that the expression you've posted isn't necessarily unique, so it could match multiple directories (and files). To get those files/directories, you can use the glob
module
import glob
for result in glob.iglob('/path/to/one/*/dir/and/*/and/*'):
find('myFile.txt', result)
I suppose that you may want to check that result
is a directory before trying to find a file in it... (os.path.isdir(result)
)
Regarding question 2:
I think you probably just want to know if the file you are looking for is in
files and then print/yield
the full path:
def find(filename, path):
for root, dirs, files in os.walk(path):
if filename in files:
print(os.path.join(root, filename))
There is nothing "unpythonic" about writing if x == y
-- It's actually quite common. However, in your case, you didn't really need to do it. The pythonic thing is to use builtin operators to look for the object you want in the list and simply construct the output based on whether you found something or not. Also, I hinted at it above, but it's often nicer (and more re-useable) to yield
the results instead of just printing them:
def find(filename, path):
for root, dirs, files in os.walk(path):
if filename in files:
yield os.path.join(root, filename)
Now, you can print it in the caller (or do other things with it...):
for filename in find(needle, haystack):
print(filename)