Discuss / Python / 打卡

打卡

Topic source

第二题

import os

dir = os.path.abspath('.')

def fileFound(seq, filedir):

    li = os.listdir(filedir)

    for x in li:

        path = os.path.join(filedir,x)

        if os.path.isfile(path) and seq in x:

            print(os.path.abspath(path).replace(dir,''))

        if os.path.isdir(path):

            fileFound(seq, path)

fileFound('py',dir)

皆由唧唧

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

这个用递归写好像简单很多

想问一下为什么将find_file(dirs_path,s)替换成 return find_file(dirs_path,s)不能得到结果?

想问一下为什么将 fileFound(seq, path)替换成return fileFound(seq, path)不能得到结果?

ChaselZ0429

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

请问为何输入绝对路径也不行呢?

import os,logging#logging.basicConfig(level=logging.INFO)path=r'/Users/chaselchou/PycharmProjects/test/'#输出path路径下所有文件与文件夹print('All:',[x for x in os.listdir(path)])# 输出path路径下所有文件夹print('dir:',[x for x in os.listdir(path) if os.path.isdir(x)])#使用walk方式输出path路径下所有文件夹for root,dirs,files in os.walk(path):    print('walk_dir:',dirs)#传入绝对路径for x in os.listdir(path):    print(os.path.isdir(os.path.join(os.path.abspath(x),x)))

结果如下:

All: ['temp', 'ff.txt', 'tt.py']

dir: []

walk_dir: ['temp']

walk_dir: []

False

False

False

ChaselZ0429

#6 Created at ... [Delete] [Delete and Lock User]
import os,logging
#logging.basicConfig(level=logging.INFO)
path=r'/Users/chaselchou/PycharmProjects/test/'
#输出path路径下所有文件与文件夹
print('All:',[x for x in os.listdir(path)])
# 输出path路径下所有文件夹
print('dir:',[x for x in os.listdir(path) if os.path.isdir(x)])
#使用walk方式输出path路径下所有文件夹
for root,dirs,files in os.walk(path):
    print('walk_dir:',dirs)
#传入绝对路径
for x in os.listdir(path):
    print(os.path.isdir(os.path.join(os.path.abspath(x),x)))

  • 1

Reply