I have a folder with around 90 subfolders. Each of these subfolders represents a region in a country and they have the same internal structure. Now, I need two shapefiles from folder (let's call it /NEED/) and shapefiles need_one.shp and need_two.shp. These shapefiles are identical just in different folders (regions) and I want to merge them as one. So, the structure is like this - main_folder/region1/NEED/file_names.shp (for example the end could say need_one.shp or need_two.shp). And the next would be main_folder/region2/NEED/file_names.shp and so on.
with open("/main_folder/region1/NEED/need_one.shp", 'w') as file:
for dir_, dirpath, filename in os.walk("/main_folder/"):
file = os.path.join(dir_, filename)
file.write("main_folder/Merged_Results/need_one.shp")
Now, this is what I have so far. I am still learning python and also using Python 3. I have created a virtual env for this and installed several needed packages.
What am I doing wrong?
When I run it, I get the following error:
TypeError: join() argument must be str or bytes, not 'list
The second version goes like this:
dirpath = "/main_folder/"
regexp = '.shp'
def iter_matching(dirpath, regexp):
with open("/main_folder/region1/NEED/need_one", 'w') as file:
for dir_, dirpath, filename in os.walk(dirpath):
abspath = os.path.join(dir_, filename)
if regexp.match(abspath):
yield abspath
file.write("/output_folder/need_one")
This runs but gives me no results.
joinerror message is caused by the line where you doos.path.join, then try printing out_dirandfilenamebefore it to enlighten you as to the error message. You could also use a python debugger to stop the code at that point and show you. At this point this is a Python question and doesn't really have any GIS aspect so might be better on stackoverflow... – Spacedman May 09 '20 at 11:58withto open a file asfile, which is what you do if you are intending to read or write from that file inside thewithblock, and then two lines later overwriting thefilevariable with something else. You might find an answer on SO by searching for "Python find files matching pattern" – Spacedman May 09 '20 at 12:01