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

2025-05-09 17:28:00

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 datapath指定路径;filetype指定文件类型。

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

2、#定义函数用于,获得文件名def get_filename(path,f坡纠课柩iletype): 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批量打开文件以及获取文件名、目录及路径
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢