Discuss / Python / 作业

作业

Topic source

Junhong1994

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

第一题dir-l我开始以为是目录减一,前思后想,难道是输出当前目录的上一层目录? 之后经过我缜密的思考及细致的观察后发现,'l'TMD不是'1',是'L'啊!!!

1、

import os
def dir_l(path = '.'):
    if path == '.':
        L = os.listdir(os.path.abspath(path))
    else:
        L = os.listdir(path)
    for file in L:
        if os.path.isdir(file):
            print(file)

path = input('请输入一个路径(无输入则默认为当前文件所在路径):')
dir_l(path)

2、

import os
def findFile(fileName):
    search = 0
    for root,dir,files in os.walk(os.path.abspath('.')):#os.walk()遍历当前目录
        for file in files:
            if fileName in file:
                relativePath = os.path.join(root.replace(os.path.abspath('.'),''),file)
                print(relativePath)
                search += 1
    if search == 0:
        print('找不到相关文件。')
    return

fileName = input('请输入要查找的关键字:')
findFile(fileName)

怎么输入代码块啊

狐的狸

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

学过Linux都应该知道这个是-l参数


  • 1

Reply