This is an example that searches PDF files in the current directory.
import os, os.path
import re
def print_pdf (arg, dir, files):
for file in files:
path = os.path.join(dir, file)
path = os.path.normcase(path)
if re.search(r".*\.pdf", path):
print path
os.path.walk('.', print_pdf, 0)
Could anyone explain what r".*\.pdf" means here?
Why ".*\"?
Thanks!
Wrong question, you missed a crucial character of the expression. 😉
In fact,
.*will match any character (.in regex), as many times as possible (*in regex; relates to the previous string, so.in this case).\., on the other hand, will match exactly one dot (.).\escapes the following character (.) so it does no longer have its special meaning (e.g. in this case “match any character”) but rather it will be treated as-is.