Discuss / Python / 交作业

交作业

Topic source

import os folderPath = ('D:\常用\Papers') getFileName = input("Please Enter File Name ")

def searchFile(name,path): listFile = os.listdir(path) #读取目录

for folder in listFile:
    getpath = os.path.join(path,folder) #获取文件路径,包括文件名
    if os.path.isfile(getpath): #检查名称是否为目录
        #如果不是目录就检查是否有对应名称
        if name in folder:
            print(os.path.join(getpath,folder))
    else: 
        #如果名称是目录就加载子目录下所有文件名
        subFolderName = os.listdir(getpath)
        for subFile in subFolderName:
            if name in subFile:
                print(os.path.join(getpath,subFile))

searchFile(getFileName,folderPath)


  • 1

Reply