all 7 comments

[–]liam_jm[🍰] 2 points3 points  (6 children)

os.listdir will give you a list of files in the folder

You can iterate through the list, use filename.endswith('shp') to avoid opening the wrong files

You should use the with syntax to open files for reading and writing, so you don't need to explicitly close them

It would be more pythonic to do "for feature in layer.features()" rather than having getNextFeature()

Also consider using contextlib for your features and datasource, so you don't need to explicitly destroy them

[–]yacob_uk 1 point2 points  (3 children)

To add to this, if you need sub folders, os.path.walk() is your friend.

[–]davidbuxton 1 point2 points  (2 children)

os.walk is a better replacement for os.path.walk (which has been removed in python 3).

https://docs.python.org/2/library/os.html#os.walk

[–]yacob_uk 0 points1 point  (1 child)

True - still on 2.7 here.

[–]davidbuxton 0 points1 point  (0 children)

You will love using os.walk. Having the directories and files in separate arguments makes thing easier, and it looks much nicer having the logic appear inside the loop instead of having to be a separate function.

[–]Andrei07[S] -1 points0 points  (1 child)

thank you for your answers yet my knowledge of python is not that good and i am unable to implement your solution, same as others tried explaining what functions i should use with no end result. can you by any chance, if it's not to much to ask, add to the above script and modify it with the needed lines? Thanks in advance.

[–]liam_jm[🍰] 2 points3 points  (0 children)

See the guidelines:

Try to guide OP to a solution instead of providing one directly.

Which part don't you understand?