Python批量打开文件以及获取文件名、目录及路径

 时间:2026-02-17 12:31:37

1、#定义函数,用于打开指定类型文件的函数

def open_allfile(path,filetype):

    data=[]

    import glob

    import os

    read_files=glob.glob(path+'*'+filetype)

    for i in read_files:

        with open(i,'rb') as infile:

            data.append(infile.read())

    return data

path指定路径;filetype指定文件类型。

Python批量打开文件以及获取文件名、目录及路径

2、#定义函数用于,获得文件名

def get_filename(path,filetype):

    import os

    name=[]

    for root,dirs,files in os.walk(path):

        for i in files:

            if filetype in i:

                name.append(i.replace(filetype,'')) 

    return name

Python批量打开文件以及获取文件名、目录及路径

3、#测试

path='C:\\Users\\jyjh\\Desktop\\soures\high_CG_pathogen\\'

filetype='.txt'

data=open_allfile(path,filetype)

name=get_filename(path,filetype)

name

Python批量打开文件以及获取文件名、目录及路径

  • 如何在Pycharm里面安装pyperclip模块
  • Visio如何绘制IDEE1X数据库表示法
  • MFC如何设置不允许用户关闭EXCEL
  • mysql怎么获取第n条记录
  • echarts在layui中怎么用
  • 热门搜索
    幼儿简笔画大全 儿童故事大全白雪公主 无底洞怎么加点 老鸭汤的做法大全 动物世界交配大全 北大仓酒价格表大全 牛肉的做法大全 尚铭按摩椅怎么样 电脑怎么截图保存 吴承恩简介