python tutorial - Python Traversing Directories Recursively - learn python - python programming
How to get the home directory in Python ?
- This will ensure it works on all platforms. Or we can do:
os.path.basename() vs. os.path.dirname
- Both functions use the os.path.split(path) function to split the pathname path into a pair; (head, tail).
- path="/foo/bar/item"
- The os.path.dirname(path) function returns the head of the path.
- The os.path.basename(path) function returns the tail of the path.
- Note that if we have a slash('/') at the end of the path, we get different result:
- path="/foo/bar/item/"
- The os.path.dirname(path) function returns the head of the path.
- The os.path.basename(path) function returns the tail of the path.
os.walk()
- The os.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up.
- For each directory in the tree rooted at directory top, it yields a 3-tuple:
- (dirpath, dirnames, filenames)
- The dirpath is a string for the path to the directory. The dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). The filenames is a list of the names of the non-directory files in dirpath.
- Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
- In this section, we're going to use the tree below:
Learn Python - Python tutorial - Python Traversing Directories Recursively - Python examples - Python programs
dirpath:
Sample Code
Output:
dirs:
Sample Code
Output:
files:
Sample Code
Output:
Listing files in directories recursively?
Sample Code
- Suppose we are now in TREE directory, then the output should look like this:
Output
Learn Python - Python tutorial - Python Traversing Directories Recursively - Python examples - Python programs
Recursive directory traversing
- One of the answers may be to use os.walk() to recursively traverse directories.
- So, in this section, we want to print all file contents recursively using the os.walk():
- The key here is to use os.path.join() when we read the files. Note that the names in the lists contain no path components.
- To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, filename).
The output from the code:
Recursive directory traversing 2
- Here is another example. It reads in CMakeLists.txt, and generate a csv file with the targets.
Sample Code
Output:
The csv file looks like this:
Recursive directory traversing 3
- I need to find files which have more than one unit of Google Ads (I am supposed to have only one of the type per page).
- So, during the recursive file traversing, I have to include only (*.php) files not *.png, *.txt, etc. Also,
- I have to count the occurrence of the Ad-unit in a file. If a file has more than one unit, this code prints out two things : full path and the count.