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

Posted yi术家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了好久没写了 放假荒废了 呜呜呜 三十讲 最近的都越来越难相关的知识,希望对你有一定的参考价值。

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

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)

 

以上是关于好久没写了 放假荒废了 呜呜呜 三十讲 最近的都越来越难的主要内容,如果未能解决你的问题,请参考以下文章

隔壁老王整理了两个月的Java面经,全面到流泪(呜呜呜)

最近状况

20190805 B组总结

中秋节快乐呜呜呜呜呜

好久没写了

LQB201701迷宫