深度学习的时候难免需要处理各类文件文件名但是方法有很多种按自己的习惯总结了一套。**1.文件路径与文件名处理** 1.1从路径到文件名首先你需要确定数据所在根目录path/home/User/Script/Model_Test/使用pathlib将路径字符串转换为Path对象读取目录下所有文件,files是一个list类型每一项是该文件的绝对路径组成的Path对象frompathlibimportPath pathFolderPath(path)fileslist(pathFolder.glob(*.*))print(type(files))classlistfiles内容[PosixPath(/home/User/Script/Model_Test/2026_03_28_01.txt.tar.gz), PosixPath(/home/User/Script/Model_Test/2026_03_28_02.txt), PosixPath(/home/User/Script/Model_Test/2026_03_28_03.txt), PosixPath(/home/User/Script/Model_Test/2026_03_28_04.txt),]遍历文件forfileinfiles:1.2文件名处理文件名由文件名.后缀组成Path对象文件名后缀处理#绝对路径print(type(file),file)#class pathlib.PosixPath /home/User/Script/Model_Test/2026_03_28_01.tar.gz#含后缀文件名print(type(file.name),file.name)#class str 2026_03_28_01.tar.gz#文件最后一个后缀.*print(type(file.suffix),file.suffix)#class str .gz#文件名print(type(file.stem),file.stem)#class str 2026_03_28_01.tar#如果碰到.tar.gz文件没有两个后缀,因为files[0].stem是class strprint(file.stem.suffix)#AttributeError: str object has no attribute suffix#可以通过再次转化为Path()对象即可print(type(Path(file.stem).suffix),Path(file.stem).suffix\n,type(Path(file.stem).stem),Path(file.stem).stem)#class str .tar#class str 2026_03_28_01Path对象文件名匹配问题Path对象的属性Path.stem,Path.name.Path.suffix,都是字符串类型,提取.stem作为字符串处理fileStemPath(file.stem).stemprint(type(fileStem),fileStem)#class str 2026_03_28_01str.split(‘*’)法适用比较简单的文件名类型如2026_03_28_01yearfileStem.split(_)[0]mouthfileStem.split(_)[1]dayfileStem.split(_)[2]nofileStem.split(_)[3]print(type(year),year,\n,type(mouth),mouth,\n,type(day),day,\n,type(no),no)输出classstr2026classstr03classstr28classstr01从右边开始拆的str.rsplit(“*”),从左往右返回结果**正则表达式法**当文件名较为复杂的时候如abc219-DEF5687_2687.txt.tar.gz,可以使用正则表达式库re找全部符合条件的re.findall(正则表达式字符串)返回的是列表列表内每项都是找第一个符合条件的re.search(正则表达式字符串返回match对象拆分re.split(正则表达式字符串,匹配re.match(正则表达式字符串返回match对象针对match对象分组re.search().group()re.match().group()返回字符串importrefilePath(/abc219-DEF2026_03_05_14_00.txt.tar.gz)fileStemfile.stem模式说明\ddigit 单个数字\d{4}匹配连续四位数字\d匹配一个或多个数字\wword 单个字母、数字、下划线\w{4}匹配连续四位字母/数字/下划线\w匹配一个或多个连续的字母/数字/下划线\.匹配 “.”需要转义^字符串以xxx开头如^abc$字符串以xxx结尾如abc$.匹配任意单个字符.*匹配任意多个字符含0个()括号内的为群组捕获组print(d:\n,type(re.findall(r\d,fileName)),type(re.findall(r\d,fileName)[0]),re.findall(r\d,fileName),type(re.findall(r\d,fileName)[0]),\n,type(re.search(r\d,fileName)),re.search(r\d,fileName),\n,type(re.search(r\d{3},fileName)),re.search(r\d{3},fileName),\n,w:\n,type(re.findall(r\w,fileName)),re.findall(r\w,fileName),type(re.findall(r\w,fileName)[0]),\n,type(re.findall(r\w{2},fileName)),re.findall(r\w{2},fileName),\n,type(re.findall(r\w,fileName)),re.findall(r\w,fileName),\n,\.:\n,type(re.findall(r\.,fileName)),re.findall(r\.,fileName),\n,^\w:\n,type(re.findall(r^\w,fileName)),re.findall(r^\w,fileName),\n,^abc.*:\n,type(re.search(r^abc.*,fileName)),re.search(r^abc.*,fileName),\n,.*.tar$:\n,type(re.findall(r.*.tar$,fileName)),re.findall(r.*.tar$,fileName),\n)输出d:classlistclassstr[2,1,9,2,0,2,6,0,3,0,5,1,4,0,0]classstrclassre.Matchre.Match object;span(3,6),match219classre.Matchre.Match object;span(3,6),match219w:classlist[a,b,c,2,1,9,D,E,F,2,0,2,6,_,0,3,_,0,5,_,1,4,_,0,0,t,x,t,t,a,r]classstrclasslist[ab,c2,19,DE,F2,02,6_,03,_0,5_,14,_0,tx,ta]classlist[abc219,DEF2026_03_05_14_00,txt,tar]\.:classlist[.,.]^\w:classlist[abc219]^abc.*:classre.Matchre.Match object;span(0,34),matchabc219-DEF2026_03_05_14_00.txt.tar.*.tar$:classlist[abc219-DEF2026_03_05_14_00.txt.tar]正则表达式返回的的是一个match对象通过group(0)输出整个match对象的字符串类型group(n)则是patterns内第n个括号内的字符串patternsr\w\d-\w{3}(\d)_(\d)_(\d)_(\d)_(\d).*matchre.match(patterns,fileName)#一般写法加ififmatch:print(fall_data {match.group(0)},{type(match)},{type(match.group(0))}YY {match.group(1)},{type(match.group(1))}MM {match.group(2)},{type(match.group(2))}DD {match.group(3)},{type(match.group(3))}hh {match.group(4)},{type(match.group(4))}dd {match.group(5)},{type(match.group(5))})输出all_dataabc219-DEF2026_03_05_14_00.txt.tar,classre.Match,classstrYY2026,classstrMM03,classstrDD05,classstrhh14,classstrdd00,classstr2.文本文件操作open(file, mode‘r’, buffering-1, encodingNone, errorsNone, newlineNone, closefdTrue, openerNone)file: 文件路径字符串或Path对象mode: 打开模式encoding: 文本编码如’utf-8’文本编码与文件打开模式模式说明r只读默认文件必须存在w只写文件存在则清空不存在则创建a追加文件存在则追加不存在则创建x独占创建文件已存在则报错b二进制模式如rbwbt文本模式默认读写模式如rw组合使用示例rb以二进制格式只读打开wb以二进制格式写入会覆盖a以读写模式追加rb以二进制格式读写打开path/home/User/Script/Model_Test/pathFolderPath(path)fileslist(pathFloder.glob(*.txt))forfileinfiles:print(file Name ,file.name)withopen(file,r)asf:linesf.readlines()print(readlinesType ,type(lines),\n,type(lines[0]),lines[0])forlineinlines:print(len(line))cleanlineline.strip()print(len(cleanline))with是一个上下文管理器自动管理文件打开后不需要手动关闭as表示将打开的文件赋值给f**f.readlines()**表示读取整个文件返回的是一个列表每一项是该行的字符串并且它包含档尾换行**.strip()**表示移除字符串首尾的指定字符默认为空白字符输出fileNameabc-123456.txt readlinesTypeclasslistclassstrABC12345678(Defghij:klmn901 opqr:STU23456789 vwx:yza01:2345678-9012345)74733.CSV文件操作安装pandas库condainstallpandaspd.read_csv(file_path, index_col0, header0) 读取CSV文件file_path - 文件路径index_col0 - 将第0列第一列作为行索引header0 - 将第0行第一行作为列名表达式说明返回类型df.loc[primary_key, attribute]行名primary_key列名attribute定位数据由csv文件决定df.loc[primary_key]整行DataFramedf.loc[[primary_key1, primary_key2]]多行DataFramedf.loc[:, attribute]整列DataFramedf.loc[:, [attribute1, attribute2]]多列DataFramedf.loc[primary_key, attribute]对应的值具体类型由csv文件决定df.loc[[primary_key1, primary_key2], [attribute1, attribute2]]多个值第一个列表是行值第二个列表是列值DataFrameimportpandasaspd path/abc.csvdfpd.read_csv(path,index_col0,header0)print(df.loc[a,b])