Skip to content

Instantly share code, notes, and snippets.

@sigmapie8
Created May 6, 2019 19:34
Show Gist options
  • Save sigmapie8/df9801a8d8a8fffdabd0b17741300f82 to your computer and use it in GitHub Desktop.
Save sigmapie8/df9801a8d8a8fffdabd0b17741300f82 to your computer and use it in GitHub Desktop.
recursing_folder
import os
source_location = "<location\\of\\the\\folder\\where\\configuration\\files\\are\\kept"
#Note how \\ is used. It is neccessary that you use \\ instead of \ for window file paths
#os.walk allows us to iterate through all the files and folder in the
#given location
for i in os.walk(source_location):
# What os.walk gives us in return is a list.
# That list has 3 elements
# path - the path of the folder and files
# dirs - a list of all the folders in that path
# files - a list of all the files in that path
# so our 'i' should look something like this:
# ["path", [all the dirs], [all the files]]
path, dirs, files = i
# Here we're taking 3 variables and assiging them to the 3 elements inside our
# list. Hence
# path = "path"
# dirs = [all the dirs] - should be empty as our folder contains no folders inside it
# files = [all the files]
# As we now have the list of all the files inside our folder, let's use that list to
# print all the files
for file in files:
print(file)
# now that we can recurse through all the files in the folder, we can do more instead of just
# printing them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment