Discuss / Python / 作业

作业

Topic source
import os

def my_search(s,root='.'):
    dirs=[]
    matchfiles=[]
    for currentName in os.listdir(root):
        addRootName=os.path.join(root,currentName)
        if os.path.isdir(addRootName):
            dirs.append(addRootName)
        elif os.path.isfile(addRootName):
            matchfiles.append(addRootName)
    for f in matchfiles:
        print(f)
    for d in dirs:
        my_search(s,d)


my_search('test','.')
my_search('ss','/')

手de温度

#2 Created at ... [Delete] [Delete and Lock User]
for f in matchfiles if s in os.path.split(f)[1]:

这句这样写就报错只能像下面这样写了,希望懂的人给我说下怎么回事。

import os
def my_search(s, root = '.'):
    dirs = []
    matchfiles = []
    for currentName in os.listdir(root):
        addRootNmae = os.path.join(root, currentName)
        if os.path.isdir(addRootNmae):
            dirs.append(addRootNmae)
        else:
            matchfiles.append(addRootNmae)
    for f in matchfiles:
        if s in os.path.split(f)[1]:
            print(f)
    #print([f for f in matchfiles if s in os.path.split(f)[1]])
    for d in dirs:
        my_search(s, d)

有些文件夹是有访问限制的,报错是正常


  • 1

Reply