lundi 30 mars 2015

How to process files from one subfolder to another in each directory using Python?

I have a basic file/folder structure on the Desktop where the "Test" folder contains "Folder 1", which in turn contains 2 subfolders:



  • An "Original files" subfolder which contains shapefiles (.shp).

  • A "Processed files" subfolder which is empty.


I am attempting to write a script which looks into each parent folder (Folder 1, Folder 2 etc) and if it finds an Original Files subfolder, it will run a function and output the results into the Processed files subfolder.


I made a simple diagram to showcase this where if Folder 1 contains the relevant subfolders then the function will run; if Folder 2 does not contain the subfolders then it's simply ignored:


Folder structure


I looked into the following posts but having some trouble:



The following is the script which seems to run happily, annoying thing is that it doesn't produce an error so this real noob can't see where the problem is:



import os, sys

from os.path import expanduser
home = expanduser("~")

for subFolders, files in os.walk(home + "\Test\\" + "\*Original\\"):
if filename.endswith('.shp'):

output = home + "\Test\\" + "\*Processed\\" + filename

# do_some_function, output

Aucun commentaire:

Enregistrer un commentaire