Discuss / Python / 练习2

练习2

Topic source

米粽粽

#1 Created at ... [Delete] [Delete and Lock User]
import os

def finder(pattern, root='.'):
    matches = []
    dirs = []

    for x in os.listdir(root):
        if os.path.isdir(os.path.join(root, x)):
            dirs.append(x)

        elif os.path.isfile(os.path.join(root, x)) and pattern in x:
            matches.append(x)

    for match in matches:
        print(os.path.join(root, match))

    for dir in dirs:
        finder(pattern, root=os.path.join(root, dir))
>>> finder('rpm')
.\Lib\distutils\command\bdist_rpm.py
.\Lib\distutils\command\__pycache__\bdist_rpm.cpython-34.pyc
.\Lib\distutils\tests\test_bdist_rpm.py
.\Lib\site-packages\setuptools\command\bdist_rpm.py
.\Lib\site-packages\setuptools\command\__pycache__\bdist_rpm.cpython-34.pyc
>>>

  • 1

Reply