我的文件夹结构如下:
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-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])
相关问题 更多 >
编程相关推荐