Python——1.2 办公自动化之 遍历、搜索及查询文件

遍历文件夹

os.walk(指定的绝对路径或相对路径) 可以把文件夹里的文件里……的文件都找出来

import os
#os.walk(绝对路径或相对路径)
for dirpath , dirname , file in os.walk('./'):
    print(f'发现文件夹{dirpath}')
    print(file)

输出结果:
发现文件夹./
[‘exercise210217.py’, ‘for循环练习1-4.py’, ‘pa.py’, ‘sublime_test.py’, ‘test1.py’, ‘test2.py’, ‘test8_2.py’, ‘test8_4.py’, ‘test8_4_2.py’, ‘test8_5_1.py’, ‘work2.py’, ‘work2.rar’, ‘个人所得税.py’, ‘个人所得税.rar’, ‘游戏.py’, ‘游戏.rar’]
发现文件夹./这是个空文件夹
[]
dirpath:文件夹路径
dirname:是dirpath文件夹下子文件夹列表
file:是dirpath文件夹下文件列表

搜索、匹配文件名称

利用字符串内置的搜索开头字符或结尾字符方法
.startswich()
.endswich()

print('abc.txt'.startswich('ab'))
print('abc.txt'.endswich('.txt'))

字符串A.startswich(字符串B):字符串A是否以字符串B开头
字符串A.endswich(字符串B):字符串A是否以字符串B结尾

glob模块
glob.golb()

模式意义
*匹配所有
匹配任何单个字符
[seq]匹配seq中的任何字符
[!seq]匹配任何不在seq中的字符
import glob
print(glob.glob('*.py'))

输出结果:
[‘exercise210217.py’, ‘for循环练习1-4.py’, ‘pa.py’, ‘sublime_test.py’, ‘test1.py’, ‘test2.py’, ‘test8_2.py’, ‘test8_4.py’, ‘test8_4_2.py’, ‘test8_5_1.py’, ‘work2.py’, ‘个人所得税.py’, ‘游戏.py’]
结果以列表方式输出
‘*’号表示可以是任意字符

import glob
print(glob.glob('le*1.py'))

输出结果:[lesson1.py]
寻找以le开头l结尾的py文件

搜索当前文件夹下包含子文件夹下的文件

import glob
print(glob.glob('**/*.py',recursive = True))

输出结果:
[‘exercise210217.py’, ‘for循环练习1-4.py’, ‘pa.py’, ‘sublime_test.py’, ‘test1.py’, ‘test2.py’, ‘test8_2.py’, ‘test8_4.py’, ‘test8_4_2.py’, ‘test8_5_1.py’, ‘work2.py’, ‘个人所得税.py’, ‘游戏.py’, ‘这是个空文件夹\none.py’, ‘这是个空文件夹\空文件夹\glob.py’]

glob.glob(’**/*.py’,recursive = True)

用 **表示任意层文件夹或文件
recursive = True 会不断进入文件夹内

fnmatch模块
可以用来匹配文件名

import fnmatch
print(fnmatch.fnmatch('test1.py','te*1.py'))
print(fnmatch.fnmatch('test1.py','test[0-9].py'))

输出结果:
True
True
如果文件夹中存在前面的文件就返回True

查询文件信息
os.scandir()返回的文件都可以查询信息

import os
for file in os.scandir():
	print(file.name,file.stat())

输出结果:
exercise210217.py os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=922, st_atime=1613565836, st_mtime=1613565836, st_ctime=1613559731)
for循环练习1-4.py os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=631, st_atime=1612357616, st_mtime=1612357616, st_ctime=1612353722)
pa.py os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0, st_atime=1614166499, st_mtime=1614166499, st_ctime=1614166499)

文件信息属性:

属性名称说明
st_size文件的体积大小(单位:bytes),除于1024就是KB
st_atime文件的最近访问时间
st_mtime文件的最近修改时间
st_ctimewindows下创建的时间
st_birthtime在Mac、linux下使用,表示创建时间

单独查询指定文件
os.stat(指定文件路径)

import os
print(os.stat('test1.py'))

输出结果:
os.stat_result(st_mode=33206, st_ino=281474977216670, st_dev=3088097945, st_nlink=1, st_uid=0, st_gid=0, st_size=466, st_atime=1614429083, st_mtime=1614429083, st_ctime=1610954152)

UNIX时间戳

st_atime=1614166499, st_mtime=1614166499, st_ctime=1614166499
时间数字叫做UNIX时间戳
通过python转换正常时间

time模块

import time
print(time.ctime(1614166499))

输出结果:
Wed Feb 24 19:34:59 2021

datetime模块

import datetime

that_time = datetime.datetime.fromtimestamp(1614166499)
print(that_time)
print(that_time.hour,that_time.minute,that_time.second)

输出结果:
2021-02-24 19:34:59
19 34 59

综合应用

要求:

  1. 搜索这个文件夹,包括文件夹内的所有文件夹
  2. 筛选体积大于100MB的压缩包.rar文件
  3. 筛选这些文件中日期晚于2020年之后的文件
  4. 输出这些文件的路径
import os
import glob
import datetime

paths = glob.glob('**/*.rar',recursive = True)

for path in paths:
    file_size = os.stat(path).st_size/1024/1024
    file_build_year = datetime.datetime.fromtimestamp(os.stat(path).st_ctime).year
    if (file_size > 100) and (file_build_year > 2020):
        print(path)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值