第30讲:文件系统:介绍一个高大上的东西 | 学习记录(小甲鱼零基础入门学习Python)

(标答出处: 鱼C论坛)
《零基础入门学习Python》

测试题:
在这里插入图片描述

def tongjidangqianwenjianshu() :
    import os
    import os.path

    all_files = os.listdir(path='.')
    dict1 = dict()

    for i in all_files :
        if os.path.isdir(i) :
            name = '文件夹' 
            if name in dict1 :
                dict1[name] += 1
            else :
                dict1[name] = 1
        else :
            kuozhanming = os.path.splitext(i)
            name = kuozhanming[1]
            if name in dict1 :
                dict1[name] += 1
            else :
                dict1[name] = 1

    for name in dict1 :
        print ("在该文件夹中共有类型为【%s】的文件 %d 个" %(name,dict1[name])) 

tongjidangqianwenjianshu()

在这里插入图片描述

def tongjidangqianwenjiandaxiao() :
    import os
    import os.path

    all_files = os.listdir(path='.')
    dict1 = dict()

    for i in all_files :
        if os.path.isfile(i) :
            size = os.path.getsize(i)
            dict1[i] = size
        else :
            continue
        
    for i in dict1 :
        print ("%s 【%sBytes】" % (i,dict1[i]))

tongjidangqianwenjiandaxiao()

在这里插入图片描述

def check_file_exist(mulu,mubiao) :
    import os
    import os.path
    
    os.chdir(mulu)
    mulu = os.listdir(os.curdir)
    for each_one in mulu :
        if each_one == mubiao :
            print (os.getcwd() + os.sep +each_one)
        if os.path.isdir(each_one) :
            check_file_exist(each_one,mubiao)
            os.chdir(os.pardir)

mulu = input ("请输入待查找的初始目录:")
mubiao = input ("请输入需要查找的目标文件:")
check_file_exist(mulu,mubiao)

在这里插入图片描述

import os
import os.path
def check_file_exist(mulu,mubiao) :
 
    os.chdir(mulu)
    mulu = os.listdir(os.curdir)
    for each_one in mulu :
        name = os.path.splitext(each_one)[1]
        if name == mubiao :
            vedio_list.append(os.getcwd() + os.sep +each_one)
        if os.path.isdir(each_one) :
            check_file_exist(each_one,mubiao)
            os.chdir(os.pardir)
        
mulu = input ("请输入待查找的初始目录:")
mubiao = [".mp4",".rmvb","avi"]
vedio_list = []
program_dir = os.getcwd()
check_file_exist(mulu,mubiao)
f1 = open (program_dir + os.sep + 'vediolist.txt','w')
f1.writelines(vedio_list)
f1.close() 

在这里插入图片描述

import os
import os.path

def display (key_dict) :
    keys = key_dict.keys()
    keys = sorted(keys)
    for each_key in keys :
        print ("关键字出现在第 %s 行,第 %s 个位置。" %(each_key,str(key_dict[each_key])))

def pos_in_line (line ,key) :
    pos = []
    begin = line.find(key)
    while begin != -1:
        pos.append(begin+1)
        begin = line.find(key,begin+1)
    return pos

def search_in_file(file_name,key) :
    f = open (file_name)
    count = 0
    key_dict = dict()
    for each_line in f :
        count += 1
        if key in each_line :
            pos = pos_in_line (each_line,key)
            key_dict[count] = pos
    f.close()
    return key_dict

def search_files (key,detail) :
    all_files = os.walk (os.getcwd())
    txt_files = []
    for i in all_files :
        for each_key in i[2] :
            if os.path.splitext(each_file)[1] == '.txt' :
                each_file = os.path.join(i[0],each_file)
                txt_files.append(each_file)
    for each_text_file in txt_files :
        key_dict = search_in_file(each_text_file , key)
        if key_dict :
            print ("===============================================")
            print ("在文件【%s】中找到关键字【%s】" %(each_txt_file ,key))
            if detail in ['YES','Yes','yes'] :
                display (key_dict)

key = input ("请将该脚本放于带查找的文件夹暖内,请输入关键字;")
detail = input ("请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):" %key)
search_files(key,detail)

标答:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38970783/article/details/85340420