0

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?

2 Answers2

2

list has no attribute endswith because you've made test a list of lists. You should be assigning to test, what you are currently assigning to res, or you should be iterating over res instead of test.

Right now, the value of test is a list whose first and only element is another list, which contains all the files which start with the string-value of i.

You can do something like:

for i in range(1,13):
    check = str(i)
    # all files starting with the string-value of `i`
    name_matches = [idx for idx in merge_list if idx.lower().startswith(check.lower())]

    for file in name_matches:
        # all files that start with `i` AND end with `.shp`
        name_ext_matches = []
        if file.endswith('.shp'):
            name_ext_matches.append(file)
        
        print(name_ext_matches)
vijaysharma
  • 21
  • 1
  • 5
0

Solved it by adding an underscore at the end of the number:

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)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 03 '23 at 03:29