Discuss / Python / 第二题

第二题

Topic source
import os
def find(path,name):
    os.chdir(path)
    files = [x for x in os.listdir(path) if os.path.isfile(x)]
    for x in files:
        if name in x:
            print("Find it in %s"%path+"\\"+x)
    dirs = [x for x in os.listdir(path) if os.path.isdir(x)]
    for dir in dirs:
        find(path+"\\"+dir,name)
os.getcwd()
os.chdir("E:\\Python")
os.getcwd()
find(os.getcwd(),".py")

输出:

Find it in E:\Python\doubanSpider.py Find it in E:\Python\first.py Find it in E:\Python\random.py Find it in E:\Python\WhileTest.py Find it in E:\Python\FishC\2016-1-29.py Find it in E:\Python\FishC\BaiduGrils.py Find it in E:\Python\FishC\ProxyIP.py Find it in E:\Python\FishC\UrlError.py Find it in E:\Python\FishC\正则表达式.py Find it in E:\Python\FishC\正则表达式1.py Find it in E:\Python\FishC\正则表达式2.py Find it in E:\Python\FishC\正则表达式3.py Find it in E:\Python\FishC\煎蛋网爬虫.py Find it in E:\Python\FishC\爬图片.py Find it in E:\Python\FishC\爬虫基础知识.py Find it in E:\Python\FishC\猫扑.py Find it in E:\Python\FishC\百度贴吧图片.py Find it in E:\Python\Ipython\liaoxuefeng\Fundation_Function.py Find it in E:\Python\Ipython\liaoxuefeng\MetaClass.py Find it in E:\Python\py_tools\forward_server.py Find it in E:\Python\py_tools\forwarder\configs.py Find it in E:\Python\py_tools\forwarder\configs.pyc Find it in E:\Python\py_tools\forwarder\core.py Find it in E:\Python\py_tools\forwarder\core.pyc Find it in E:\Python\py_tools\forwarder\forwarder.py


  • 1

Reply