拆分文件名以查找我要中断的文件

2024-06-16 13:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的文件夹结构如下:

TestOpt > roll_1_oe_2017-03-10
        > roll_2_oe_2017-03-05
           :           :
        > roll_600_oe_2012-05-10

TestOpt是主文件夹,roll\\uoe\\eem>是保存.csv记录的子文件夹,如果它们在roll的某个范围内,我会询问它们

我正在尝试分析文件名,因为我只希望查询子文件夹的卷数大于500的记录(因此我想查询文件夹roll_500_oe_2012-05-10roll_600_oe_2012-05-10中的记录,包括在内)

我尝试过按"_"分割文件夹名,以便检索卷号,但我遇到了一个问题,因为我无法使代码超过TestOpt文件名。请参见下面的代码:

rootdir = r'C:/Users/Stacey/Documents/TestOpt/'

     #cycle through all the folders in the TestOpt directory
     for dirName,sundirList, fileList in os.walk(rootdir):
         #print('Found directory: %s' % dirName)
         #split the file name by _ 
         x = dirName.split("_")
         print('list length ',len(x))
         #If the length of the folder name is greater than 1 its not the TestOpt folder
         if len(x) > 1:
             #the second split list element is the roll number
             roll = x[2]
             #interrogate records in folder id roll is greater or equal to 500
             if roll >= 500:
                 print('myroll1 ',roll)
                 for fname in fileList:
                     do something....

如果有人能提供任何帮助,我将不胜感激

谢谢


Tags: the代码in文件夹is文件名记录folder
1条回答
网友
1楼 · 发布于 2024-06-16 13:20:38

您需要显式地声明roll是一个整数,因为从文件名生成的列表是一个字符串列表

使用roll = int(x[2])

相关问题 更多 >