What I am trying to achieve is creating lists for each file that begins with a certain number and ends with ".shp". One list for all files that begins with 1 and ends with ".shp", one for all files that begins with 2 and ends with ".shp" and so on.
This is the snippet I'm working with:
from os.path import normpath
import os
path_temp2 = normpath(r'C:\\Users\\tlind\\Dropbox\\Documents\\Temp\\temp2layers\\')
merge_list = os.listdir(path_temp2)
for i in range(1,13):
test = []
check = str(i)
res = [idx for idx in merge_list if idx.lower().startswith(check.lower())]
test.append(res)
for file in test:
test2 = []
if file.endswith('.shp'):
test2.append(file)
print(test2)
It returns: AttributeError: 'list' object has no attribute 'endswith'
EDIT: This almost solves it:
from os.path import normpath
import os
path_temp2 = normpath(r'C:\\Users\\tlind\\Dropbox\\Documents\\Temp\\temp2layers\\')
merge_list = os.listdir(path_temp2)
for i in range(1,13):
check = str(i)
name_ext_matches = []
name_matches = [idx for idx in merge_list if idx.lower().startswith(check.lower())]
for file in name_matches:
if file.endswith('.shp'):
name_ext_matches.append(file)
print(name_ext_matches)
There's a big DOH in this one. The first list includes 10, 11 and 12 of course. I need to find a way to work my way around that. Any one with any suggestions?