拆分文件名以查找我要查询的文件



我有一个文件夹结构如下:

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

TestOpt 是主文件夹和卷__oe_是保存.csv记录的子文件夹,如果它们在一定的卷筒范围内,我希望询问这些记录。

我正在尝试分析文件名,因为我只想查询子文件夹卷大于 500 的记录(所以我想查询文件夹roll_500_oe_2012-05-10中的记录roll_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....

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

谢谢

你需要明确声明roll是一个整数,因为从文件名组成的列表是一个字符串列表。

使用roll = int(x[2]).

最新更新