Discuss / Python / 第二问

第二问

Topic source

露心

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

import os

def find(s, name):

    d = [x for x in os.listdir(s) if os.path.isdir(os.path.join(s, x))]#当前目录下的所有子目录

    f = [x for x in os.listdir(s) if os.path.isfile(os.path.join(s, x)) and name in os.path.splitext(x)[1]]

    for x in f:

        print(os.path.join(s,x))

    for x in d:

        g = os.path.join(s, x)

        find(s, name)#目录递

范伟

#2 Created at ... [Delete] [Delete and Lock User]

你好,我想问一下,为什么不直接用d = os.listdir(s),而用 d = [x for x in os.listdir(s) if os.path.isdir(os.path.join(s, x))]

Wuli_杰杰

#3 Created at ... [Delete] [Delete and Lock User]

d = os.listdir(s) 返回指定的文件夹包含的文件或文件夹的名字的列表,而我们需要的是目录,所以加一个os.path.isdir(os.path.join(s, x)) 进行判断


  • 1

Reply