0

我最终需要显示我的列表中文件以 .shp 结尾的所有项目。所以我需要能够分别索引每个列表项。有什么建议么?

这是我到目前为止所拥有的:

folderPath = r'K:\geog 173\LabData'

import os
import arcpy

arcpy.env.workspace = (folderPath)
arcpy.env.overwriteOutput = True

fileList = os.listdir(folderPath)
print fileList


"""Section 2: Identify and Print the number
and names of all shapefiles in the file list:"""

numberShp = 0

shpList= list()

for fileName in fileList:
    print fileName

fileType = fileName[-4:]
print fileType

if fileType == '.shp':
    numberShp +=1
    shpList.append(fileName)

print shpList
print numberShp
4

2 回答 2

1

您可以使用列表推导很容易地做到这一点,并且str.endswith()

shpList = [fileName for fileName in fileList if fileName.endswith('.shp')]

print shpList
print len(shpList)
于 2013-10-09T05:08:34.800 回答
0

你能请指定所需的输出格式。这样工作就轻松了...

一个可能的答案是

fileList = [f for f in os.listdir('K:\geog 173\LabData') if f.endswith('.shp')]

for i,val in enumerate(fileList):
    print '%d. %s' %(i,val)  

#If u want to print the length of the list again...
print len(fileList)  
于 2013-10-09T05:23:50.243 回答