Discuss / Python / 第一题不懂什么意思

第一题不懂什么意思

Topic source

幻云风--

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

第二题

import os str_ = 'answer'
os.chdir(r'c:\users\win7\desktop\python文件') #测试用目录

def find():

#查找当前目录下包含'answer'的文件并打印
filepath_list = [x for x in os.listdir() 
if os.path.isfile(x) and str_ in x]     
for i in filepath_list:
    print(i)
#查找当前目录的子目录
subfolder_list =[x for x in os.listdir() if os.path.isdir(x)]
#在子目录中循环
for i in subfolder_list:
    os.chdir(i)     #切换当前目录为子目录i
    find()          #递归调用
    os.chdir('..')  #切换目录为上一级目录,还原为初始目录

find()


  • 1

Reply