There are similar questions to this but not quite what im looking for.
Having created a list with all files from a specific path im looking to filter out anything that does not contain a specific sequence in a string.
someFiles = os.listdir(somePath)
listSize = len(someFiles)
for i in range(0, listSize):
if (".py" not in someFiles):
for i in range(0, listSize): if (".py" not in someFiles): someFiles.remove(i)
Note that you are trying to remove
i from the list.
i will be an index of an element in the list (
1, etc) and not an actual element. This will raise an error.
Simply use list comprehension to get only the files you do need:
required_files = [f for f in someFiles if '.py' in f]
You could also use
filter but it will require a lambda (and also note it will return a
filter object and not a
required_files = list(filter(lambda x: '.py' in x, someFiles))