ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

好久没写了 放假荒废了 呜呜呜 三十讲 最近的都越来越难

2021-06-18 20:04:14  阅读:203  来源: 互联网

标签:file 荒废 dict 好久没 key each path os 放假


------------恢复内容开始------------

0:

import os

all_files = os.listdir(os.curdir)
type_dict = {}
type_dict['文件夹'] = 0
for each in all_files:
    if os.path.isdir(each):
        type_dict['文件夹'] += 1
    else:
        suffix = os.path.splitext(each)[1]
        type_dict.setdefault(suffix, 0)
        type_dict[suffix] += 1

print(type_dict)
for each_doc in type_dict:
    print('该文件夹下共有类型为'+each_doc+'的文件%d个'%type_dict[each_doc])

1:

all_files = os.listdir(os.curdir)
# type_dict = {}
# type_dict['文件夹'] = 0
for each in all_files:
    print(each,'[%d]Bytes'%os.path.getsize(each))

2:

 在一个目录中查文件  哈哈哈 这个是我自己写的  开心 好难。

def search_file(file_name,path):
    all_files = os.listdir(path)
    a = None
    for each in all_files:
        if each == file_name and a== None:
            return path
        else:
            if os.path.isdir(os.path.join(path,each)) and a== None:
                a = search_file(file_name,os.path.join(path,each))
    if a != None :
        return a


path = input('请输入待查找的初始目录:')
file_name = input('请输入需要查找的目标文件:')
print(search_file(file_name,path))

3:

def search_file(path,target):
    all_files = os.listdir(path)
    for each in all_files:
        if os.path.isdir(os.path.join(path,each)) :
            a = search_file(os.path.join(path,each),target)
        else:
            if os.path.splitext(each)[1] in '.mp4,.rmvb,.avi,.mp3':
                target.write(os.path.join(path,each))
                target.write('\n')


f = open('vedioList.txt','w')
path = input('请输入待查找的初始目录:')
print(search_file(path,f))

啊啊啊  信心再次增强!!!

4:  对不起 信心没了  抄答案

 

def print_pos(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) # 用户的角度是从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() # 字典,用户存放key所在具体行数对应具体位置

    for each_line in f:
        count += 1
        if key in each_line:
            pos = pos_in_line(each_line, key) # key在每行对应的位置
            key_dict[count] = pos

    f.close()
    return key_dict


def search_files(key, detail):
    all_files = os.walk(os.getcwd())
    #walk 行走至目录 而且会将文件分类 返回一个迭代对象  0是当前目录 1是文件夹列表 2是文件列表      os.getcwd 返回当前进程目录。
    txt_files = []

    for i in all_files:
        for each_file 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_txt_file in txt_files:
        key_dict = search_in_file(each_txt_file, key)
        if key_dict:
            print('================================================================')
            print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))
            if detail in ['YES', 'Yes', 'yes']:
                print_pos(key_dict)


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

 

标签:file,荒废,dict,好久没,key,each,path,os,放假
来源: https://www.cnblogs.com/xiaoli1996/p/14901030.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有