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 Function in Python?
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.
- Python Programming – Recursion
- How to Recursively Copy a Folder (Directory) in Python
- Python Programming – Date and Time Functions
Recursive Python Function: Let’s Code!
Let’s write the code of traversal within a function, which looks like this:
import os def print_movie_files(movie_directory, movie_extensions=['avi', 'dat', 'mp4', 'mkv', 'vob']): ''' Print files in movie_directory with extensions in movie_extensions, recursively. ''' # Get the absolute path of the movie_directory parameter movie_directory = os.path.abspath(movie_directory) # Get a list of files in movie_directory movie_directory_files = os.listdir(movie_directory) # Traverse through all files for filename in movie_directory_files: filepath = os.path.join(movie_directory, filename) # Check if it's a normal file or directory if os.path.isfile(filepath): # Check if the file has an extension of typical video files for movie_extension in movie_extensions: # Not a movie file, ignore if not filepath.endswith(movie_extension): continue # We have got a video file! Increment the counter print_movie_files.counter += 1 # Print it's name print('{0}'.format(filepath)) elif os.path.isdir(filepath): # We got a directory, enter into it for further processing print_movie_files(filepath)
The code is pretty much self-explanatory along with the comments. The recursive Python function print_movie_files
takes two arguments: the directory path to search. 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 filepath is a normal file or directory using the os.path.isfile
method. If it’s a normal file with an extension in movie_extensions
, it will print the filepath. If filepath
is a directory, we recursively call the function itself to further process it.
Calling the Recursive Python Function
Now, we call this function within the __main__
scope:
Python 3.x
if __name__ == '__main__': # Directory argument supplied, check and use if it's a directory if len(sys.argv) == 2: if os.path.isdir(sys.argv[1]): movie_directory = sys.argv[1] else: print('ERROR: "{0}" is not a directory.'.format(sys.argv[1])) exit(1) else: # Set our movie directory to the current working directory movie_directory = os.getcwd() print('\n -- Looking for movies in "{0}" --\n'.format(movie_directory)) # Set the number of processed files equal to zero print_movie_files.counter = 0 # Start Processing print_movie_files(movie_directory) # We are done. Exit now. print('\n -- {0} Movie File(s) found in directory {1} --'.format \ (print_movie_files.counter, movie_directory)) print('\nPress ENTER to exit!') # Wait until the user presses enter/return, or <CTRL-C> try: input() except KeyboardInterrupt: exit(0)
Python 2.x
if __name__ == '__main__': # Directory argument supplied, check and use if it's a directory if len(sys.argv) == 2: if os.path.isdir(sys.argv[1]): movie_directory = sys.argv[1] else: print('ERROR: "{0}" is not a directory.'.format(sys.argv[1])) exit(1) else: # Set our movie directory to the current working directory movie_directory = os.getcwd() print('\n -- Looking for movies in "{0}" --\n'.format(movie_directory)) # Set the number of processed files equal to zero print_movie_files.counter = 0 # Start Processing print_movie_files(movie_directory) # We are done. Exit now. print('\n -- {0} Movie File(s) found in directory {1} --'.format \ (print_movie_files.counter, movie_directory)) print('\nPress ENTER to exit!') # Wait until the user presses enter/return, or <CTRL-C> try: raw_input() except KeyboardInterrupt: exit(0)
Running the Script
- Download and extract the source code zip file (see below), and copy
list-movies.py
to the directory you wish to search in. - — OR — copy the article code to a new file and save it as
list-movies.py
in the directory you wish to search in. cd
into the directory the movies are in. eg.cd ~/Movies
orcd C:\\Users\\Videos
.- Run the
list-movies.py
script with/path/to/python list-movies.py
- Linux/OSX/Unix:
python list-movies.py
- Windows:
C:\\Python34\\python.exe list-movies.py
- Linux/OSX/Unix:
Tip: On Linux/OSX/Unix you can mark the file as executable, add a Python shebang line at the top of the file, and run it directly. eg.