小甲鱼Python3学习笔记之第三十讲(仅记录学习)

第三十讲:文件系统,介绍一个高大上的东西

一、知识点:

0.模块,是一个包含所有你定义的函数和变量的文件,其后缀名是.py。模块可以被别的程序引入,以使用该模块中的函数等功能。

1.os模块中关于文件/目录常用的函数使用方法

函数名

使用方法

getcwd()返回当前工作目录
chdir(path)改变工作目录
listdir(path='.')列举指定目录中的文件名('.'表示当前目录,'..'表示上一级目录)
mkdir(path)创建单层目录,如该目录已存在抛出异常
makedirs(path)递归创建多层目录,如该目录已存在抛出异常,注意:'E:\\a\\b'和'E:\\a\\c'并不会冲突
remove(path)删除文件
rmdir(path)删除单层目录,如该目录非空则抛出异常
removedirs(path)递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则抛出异常
rename(old, new)将文件old重命名为new
system(command)运行系统的shell命令
walk(top)遍历top路径以下所有的子目录,返回一个三元组:(路径, [包含目录], [包含文件])

以下是支持路径操作中常用到的一些定义,支持所有平台

os.curdir指代当前目录('.')
os.pardir指代上一级目录('..')
os.sep输出操作系统特定的路径分隔符(Win下为'\\',Linux下为'/')
os.linesep当前平台使用的行终止符(Win下为'\r\n',Linux下为'\n')
os.name指代当前使用的操作系统(包括:'posix',  'nt', 'mac', 'os2', 'ce', 'java')

 2.os.path模块中关于路径常用的函数使用方法

函数名

使用方法

basename(path)去掉目录路径,单独返回文件名
dirname(path)去掉文件名,单独返回目录路径
join(path1[, path2[, ...]])将path1, path2各部分组合成一个路径名
split(path)分割文件名与路径,返回(f_path, f_name)元组。如果完全使用目录,它也会将最后一个目录作为文件名分离,且不会判断文件或者目录是否存在
splitext(path)分离文件名与扩展名,返回(f_name, f_extension)元组
getsize(file)返回指定文件的尺寸,单位是字节
getatime(file)返回指定文件最近的访问时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
getctime(file)返回指定文件的创建时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
getmtime(file)返回指定文件最新的修改时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)

以下为函数返回 True 或 False

exists(path)判断指定路径(目录或文件)是否存在
isabs(path)判断指定路径是否为绝对路径
isdir(path)判断指定路径是否存在且是一个目录
isfile(path)判断指定路径是否存在且是一个文件
islink(path)判断指定路径是否存在且是一个符号链接
ismount(path)判断指定路径是否存在且是一个挂载点
samefile(path1, paht2)判断path1和path2两个路径是否指向同一个文件

--------------------------------------------------------------------我是一条分割线----------------------------------------------------------------------

 二、课后作业:

0.编写一个程序,统计当前目录下每个文件类型的文件数:

import os

all_files = os.listdir(os.curdir)
type_dict = dict()

for each_file in all_files:
    if os.path.isdir(each_file):
        type_dict.setdefault('文件夹',0)
        type_dict['文件夹'] += 1
    else:
        ext = os.path.splitext(each_file)[1]
        type_dict.setdefault(ext,0)
        type_dict[ext] += 1

for each_type in type_dict.keys():
    print('该文件夹下共有类型为【%s】的文件%d个'%(each_type,type_dict[each_type]))

1.编写一个程序,计算当前文件夹下所有文件的大小:

import os

def file_size():
    file_name = os.listdir(os.curdir)
    dict1 = dict()
 
    for each_file in file_name:
        if os.path.isfile(each_file):
            dict1.setdefault(each_file,os.path.getsize(each_file))
            print('%s的大小为:【%d Bytes】'%(each_file,dict1[each_file]))
 
file_size()

2.编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在。如遇到文件夹,则进入文件夹继续搜
索。

import os

def search_file(start_dir, target) :
    os.chdir(start_dir)
    
    for each_file in os.listdir(os.curdir) :
        if each_file == target :
            print(os.getcwd() + os.sep + each_file) 
        if os.path.isdir(each_file) :
            search_file(each_file, target) 
            os.chdir(os.pardir) #切记返回上一层目录
 
start_dir = input('请输入待查找的初始目录:')
target = input('请输入需要查找的目标文件:')
search_file(start_dir, target)

3.编写一个程序,用户输入开始搜索的路径,查找该路径下(包含子文件夹内)所有的视频格式文件(要求查找mp4
rmvb, avi的格式即可),并把创建一个文件(VedioList.txt)存放所有找到的文件的路径。

import os

vedio_list = []

def search_file(start_dir):
    os.chdir(start_dir)
    for each_file in os.listdir(os.curdir):
        if os.path.isfile(each_file):
            file_ext = os.path.splitext(each_file)[1]
            if file_ext in ['.mp4','rmvb','.avi']:
                vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep)
        if os.path.isdir(each_file):
            search_file(each_file)
            os.chdir(os.pardir)
    return vedio_list

start_dir = input('请输入待查找的初始目录:')
vedio_list = search_file(start_file)
f = open(os.getcwd() + os.sep + 'VedioList.txt','w')
f.writelines(vedio_list)
f.close()

4.编写一个程序,用户输入关键字,查找当前文件夹内(如果当前文件夹内包含文件夹,则进入文件夹继续搜索)所有含
有该关键字的文本文件(.txt后缀),要求显示该文件所在的位置以及关键字在文件中的具体位置(第几行第几个字符)。

import os
def print_keywords(dict_keywords):
    keys = dict_keywords.keys()
    keys = sorted(keys)
    for each in keys:
        print('关键字出现在第%s行,第%s个位置'%(each,str(dict_keywords[each])))
    
def line_keywords(line, keywords):
    key_index = []
    start = line.find(keywords)
    while start!=-1:
        key_index.append(start+1)
        start = line.find(keywords, start+1)
    return key_index       
    
 
def file_keywords(filename, keywords):
    f = open(filename,'r')
    line = 0
    dict_keywords = dict()
    for each_line in f:
        line +=1
        if keywords in each_line:
                key_index = line_keywords(each_line, keywords)
                dict_keywords[line]= key_index
    f.close()
    return dict_keywords
 
    
def file_search(keywords, flag):
    all_files = os.walk(os.getcwd())
    txt_list = []
 
    for each in all_files:
        for filename in each[2]:
            if os.path.splitext(filename)[1]== '.txt':
                txt_list.append(os.path.join(each[0],filename))
 
    for each_txt_file in txt_list:
        dict_keywords = file_keywords(each_txt_file, keywords)#调用file_keywords函数
        print('====================================================')
        print('在文件【%s】中找到关键字【%s】' % (each_txt_file, keywords))
        if flag in ['YES','Yes','yes']:
            print_keywords(dict_keywords)
 
        
keywords = input("请将该脚本放于待查找的文件夹中,请输入关键字:")
flag = input("请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):")
file_search(keywords, flag)

--------------------------------------------------------------------我是一条分割线----------------------------------------------------------------------

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值