python - 快速搜索指定格式文件的方法

  • 需求1: 快速查找指定文件夹下,所有满足开头为NC,结尾为.nc的文件:
python 复制代码
root_path = "/Users/xpji/convert_kuihua9data/20230621"

from pathlib import Path

def get_filtered_file_paths(root_path):
    filtered_files = [str(file_path) for file_path in Path(root_path).rglob('NC*.nc')]
    print(filtered_files)
    return filtered_files

filtered_files = get_filtered_file_paths(root_path)
  • 需求2: 满足指定文件夹下,所有后缀为.HDF5的文件,并排序:
python 复制代码
gpm_path = r'/Datadisk/GPM/GPM_30min/2017/'
gpm_pattern = '*.HDF5'

### 方法1
# 使用列表推导式和Path对象快速获取满足条件的文件路径
gpm_list = [str(file_path) for file_path in Path(gpm_path).rglob(gpm_pattern)]
gpm_list.sort()
# 输出结果
print(gpm_list)


### 方法2
# 初始化文件路径列表
gpm_list2 = []
# 遍历所有以'001'到'031'开头的文件夹
for i in range(1, 366):
    # 构建每个文件夹的名称,如果数字小于10,则需要在前面加0
    folder_name = str(i).zfill(3)
    folder = os.path.join(gpm_path, folder_name)
    # 获取所有匹配到的文件路径
    files = glob.glob(os.path.join(folder, gpm_pattern))
    # 将匹配到的文件路径加入列表
    gpm_list2.extend(files)
gpm_list2.sort()

相比之下,第一种方法速度更快,只需要满足筛选条件后排序即可;第二种是遍历文件夹后再排序

  • 需求3: 生成指定一年的所有天数,格式为年-月-日,数据类型为date,如2017-12-31
python 复制代码
ds = []
year = 2017
for month in range(1, 13):
    days_in_month = calendar.monthrange(year, month)[1]
    
    for day in range(1, days_in_month + 1):
        date = datetime.date(year, month, day)
        ds.append(date)
        print(date)
  • 需求4 :匹配GPM和Sate卫星数据种相同时刻的数据:

    • 方法1
python 复制代码
gpm_abspath  = [os.path.abspath(i) for i in gpm_filtered_files]
sate_abspath = [os.path.abspath(i) for i in sate_filtered_files]

# 获取 gpm_list 和 sate_list 中文件名部分
gpm_dir = [os.path.basename(i) for i in gpm_abspath]
sate_dir = [os.path.basename(i) for i in sate_abspath]

match = []
for i in tqdm(range(len(gpm_dir))):
    
    index1 = gpm_dir[i].split('.')[4][:8] + ' ' + gpm_dir[i].split('.')[4][10:14] 
    
    for j in range(len(sate_dir)):
        
        index2 =  sate_dir[j].split('_')[2]  + ' ' + sate_dir[j].split('_')[3]
        
        if index1 == index2:
            
            match.append(gpm_abspath[i])
            
            print(gpm_abspath[i])
  • 方法2
python 复制代码
gpm_dir2 = [filepath.split('.')[4][:8] + ' ' + filepath.split('.')[4][10:14] for filepath in gpm_abspath]

sate_dict = {}
gpm_dict = {}

for filepath in sate_abspath:
    
    index = filepath.split('_')[2] + ' ' + filepath.split('_')[3]
    sate_dict[index] = filepath
    
for filepath in gpm_abspath:
    
    index = filepath.split('.')[4][:8] + ' ' + filepath.split('.')[4][10:14]
    gpm_dict[index] = filepath

match_sate_path = []
match_gpm_path = []
for index in tqdm(set(gpm_dir2) & set(sate_dict.keys())):
    match_sate_path.append(sate_dict[index])
    match_gpm_path.append(gpm_dict[index])
    print(sate_dict[index])
match_sate_path.sort()
match_gpm_path.sort()
  • 需求5: 保存路径为.pkl,并读取文件
python 复制代码
with open('match_gpm_list_2017.pkl', 'wb') as f:
    pickle.dump(match_gpm_path, f)
    print('match_gpm_list_2017.pkl','save finish')
    
with open('match_sate_list_2017.pkl', 'wb') as f:
    pickle.dump(match_sate_path, f)
    print('match_sate_list_2017.pkl','save finish')

with open('my_list.pkl', 'rb') as f:
    gpm_list = pickle.load(f)
相关推荐
平头哥技术团队6 分钟前
Day 16 | 用 ul、ol、li 一个标签,把散资料收成有序的技能清单页
开发语言·前端·html·html5
Cenxi15 分钟前
f-string 完全手册:60 题从入门到实战
python
统计学小王子16 分钟前
数学建模国赛倒计时 2 天 ——《统计模型精讲(广义线性模型 R 语言实例 + 分析)》
开发语言·数学建模·r语言
2601_9622998820 分钟前
Python编写Linux命令指南
linux·python·开发·命令·指南
此冬歌咏23 分钟前
自动化服务器运维监控系统(python+shell)
linux·运维·服务器·开发语言·python·自动化
zhanghaha131425 分钟前
Python进阶教程:23_Scrapy 爬虫框架 零基础超详细教程
python·信息可视化
爱读源码的大都督40 分钟前
DeepSeek面试官问:生产RAG系统回答不准确,该如何定位和优化?这样回答,能让面试官当场给你Offer!
java·后端·python
Cx330❀42 分钟前
【Linux网络】深入TCP协议:从滑动窗口到拥塞控制与性能优化全景解析
linux·开发语言·网络·tcp/ip·ai·性能优化·ai编程
“AI国潮设计-小江”1 小时前
【Python实战】SDXL精准控制“普宁英歌舞×星空蛋糕”IP落地,附核心Prompt与商用授权思路
开发语言·人工智能·python·prompt·aigc
k4m7v2pz1 小时前
从 Python 搬到 Rust:pyglet MIDI DAW 变成 egui 鬼畜采样器的迁移复盘
开发语言·python·rust