Quantcast
Channel: Python Central » Python Recipes
Viewing all articles
Browse latest Browse all 19

Recursive Python Function Example: Make a List of Your Movies!

$
0
0
This recipe is simply a recursive Python function example, using Python’s os.listdir function. This is only an example of recursion in Python, however not the most effective method to traverse a directory in Python. The built-in Python function os.walk is the most “Pythonic” method. For a quick look at how to use os.walk, checkout this article for a quick os.walk example. If you are after a more in-depth look at os.walk, be sure to checkout the article Python’s os.walk: An In-depth Guide

So. What’s better than making a list of video files on your hard disc drive?

Let’s make a list of all video files in a folder, and all other folders in it!

What is a Recursive Python Function?

Recursion is a concept in computer science. Essentially, it divides a problem into sub-problems. Recursion in Python generally relates to a specific function, method or object, which calls itself to break up these problems. For example, a factorial function would be as follows:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

Note that the factorial function calls itself, to break down the factorial problem into sub-problems.

Recursive Python Function Overview

After writing the code, save the file with a .py extension, for example movie_list_maker.py. Then put the file in a directory where you’ve got your movie files (and other files, as well). The code in the script will recursively traverse (look in) all other folders within it, and check for video files. If you’re using Windows and have Python IDLE installed, you can just double-click on the file and check the output. If you’re on Linux or Mac, open a terminal and cd into the directory where your script is, then open the script typing ./movie_list_maker.py in your terminal (make sure the file is executable first).

Recursive Python Function: Let’s Code!

Let’s write the code of traversal within a function, which looks like:

import os

def processFile(currentDir):
    ''' Process video files within this directory. '''
    # Get the absolute path of the currentDir parameter
    currentDir = os.path.abspath(currentDir)
 
    # Get a list of files in currentDir
    filesInCurDir = os.listdir(currentDir)
 
    # Traverse through all files
    for file in filesInCurDir:
        curFile = os.path.join(currentDir, file)
 
        # Check if it's a normal file or directory
        if os.path.isfile(curFile):
            # Get the file extension
            curFileExtension = curFile[-3:]
 
            # Check if the file has an extension of typical video files
            if curFileExtension in ['avi', 'dat', 'mp4', 'mkv', 'vob']:
                # We have got a video file! Increment the counter
                processFile.counter += 1
 
                # Print it's name
                print('\n%s' % curFile)
        else:
            # We got a directory, enter into it for further processing
            processFile(curFile)

The code is pretty much self-explanatory along with the comments. This recursive Python function takes one argument: the name of a directory to work on. Then it gets a list of all files and folders in this directory using the os.listdir method. We use a for loop to work on the list, check whether the file variable is a normal file or a directory using the os.path.isfile method. If it’s a normal file, we get the extension of the file using: curfile[-3:] – we assume that last three characters of the filename is it’s extension, so we’ve used a negative sign while slicing the curfile variable. For more information on slicing strings, checkout the article on cutting and slicing strings in Python.

If the filename is a directory other than a regular file, we recursively call the function itself to further process it.

Calling the Recursive Python Function

Now, we call this function within the __main__ scope:

import os

if __name__ == '__main__':
    # Get the current working directory
    currentDir = os.getcwd()

    print('Starting processing in %s' % currentDir)

    # Set the number of processed files equal to zero
    processFile.counter = 0

    # Start Processing
    processFile(currentDir)

    # We are done. Exit now.
    print('\n -- %s Movie File(s) found in directory %s --' \
          % (processFile.counter, currentDir))
    print('\n Press ENTER to exit!')

    # Wait until the user presses enter/return
    input()

import os

if __name__ == '__main__':
    # Get the current working directory
    currentDir = os.getcwd()

    print('Starting processing in %s' % currentDir)

    # Set the number of processed files equal to zero
    processFile.counter = 0

    # Start Processing
    processFile(currentDir)

    # We are done. Exit now.
    print('\n -- %s Movie File(s) found in directory %s --' \
          % (processFile.counter, currentDir))
    print('\n Press ENTER to exit!')

    # Wait until the user presses enter/return
    raw_input()

Once again, the os.getcwd method helps us to get the current working directory (cwd), i.e. the directory where the script resides. It calls our just-written function and also has a counter, it counts how many video files it found. Finally, we print all information we have, and wait for a prompt from user to terminate the program using the input or raw_input method (Python changed the name of the raw_input method to input when going from Python 2 to Python 3).

The post Recursive Python Function Example: Make a List of Your Movies! appeared first on Python Central.


Viewing all articles
Browse latest Browse all 19

Trending Articles