Rolex Pearlmaster Replica
  Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
This article is part of in the series
Published: Thursday 15th March 2012
Last Updated: Wednesday 29th December 2021
This recipe is a practical example of Python recursive functions, using the os.listdir function. However it is not the most effective method to traverse a directory in Python. os.walk is generally considered the most Pythonic method. For a quick look at how to use os.walk, checkout the article this article for a 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 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:

[python]
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
[/python]

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

Recursive Python Function: Let's Code!

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

[python]
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)
[/python]

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]
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
try:
input()
except KeyboardInterrupt:
exit(0)
[/python]



[python]
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
try:
raw_input()
except KeyboardInterrupt:
exit(0)
[/python]


Running the Script

  1. Download and extract the source code zip file (see below), and copy list-movies.py to the directory you wish to search in.
  2. -- OR -- copy the article code to a new file and save it as list-movies.py in the directory you wish to search in.
  3. cd into the directory the movies are in. eg. cd ~/Movies or cd C:\\Users\\Videos.
  4. 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

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.

[shell]
cd ~/Desktop/list-movies.py
chmod +x ./list-movies.py
# Add "#/usr/bin/env python" to the top of the file
./list-movies.py # Run script, search files in current directory
./list-movies.py ~/Movies # Run script, search for files in ~/Movies
[/shell]

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.

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 the user to terminate the program using the input() or raw_input() function (Python changed the name of the raw_input() function to input() when going from Python 2 to Python 3).

Latest Articles


Tags

  • deque
  • heap
  • Data Structure
  • howto
  • dict
  • csv in python
  • logging in python
  • Python Counter
  • python subprocess
  • numpy module
  • Python code generators
  • KMS
  • Office
  • modules
  • web scraping
  • scalable
  • pipx
  • templates
  • python not
  • pytesseract
  • env
  • push
  • search
  • Node
  • python tutorial
  • dictionary
  • csv file python
  • python logging
  • Counter class
  • Python assert
  • linspace
  • numbers_list
  • Tool
  • Key
  • automation
  • website data
  • autoscale
  • packages
  • snusbase
  • boolean
  • ocr
  • pyside6
  • pop
  • binary search
  • Insert Node
  • Python tips
  • python dictionary
  • Python's Built-in CSV Library
  • logging APIs
  • Constructing Counters
  • Assertions
  • Matplotlib Plotting
  • any() Function
  • Activation
  • Patch
  • threading
  • scrapy
  • game analysis
  • dependencies
  • security
  • not operation
  • pdf
  • build gui
  • dequeue
  • linear search
  • Add Node
  • Python tools
  • function
  • python update
  • logging module
  • Concatenate Data Frames
  • python comments
  • matplotlib
  • Recursion Limit
  • License
  • Pirated
  • square root
  • website extract python
  • steamspy
  • processing
  • cybersecurity
  • variable
  • image processing
  • incrementing
  • Data structures
  • algorithm
  • Print Node
  • installation
  • python function
  • pandas installation
  • Zen of Python
  • concatenation
  • Echo Client
  • Pygame
  • NumPy Pad()
  • Unlock
  • Bypass
  • pytorch
  • zipp
  • steam
  • multiprocessing
  • type hinting
  • global
  • argh
  • c vs python
  • Python
  • stacks
  • Sort
  • algorithms
  • install python
  • Scopes
  • how to install pandas
  • Philosophy of Programming
  • concat() function
  • Socket State
  • % Operator
  • Python YAML
  • Crack
  • Reddit
  • lightning
  • zip files
  • python reduce
  • library
  • dynamic
  • local
  • command line
  • define function
  • Pickle
  • enqueue
  • ascending
  • remove a node
  • Django
  • function scope
  • Tuple in Python
  • pandas groupby
  • pyenv
  • socket programming
  • Python Modulo
  • Dictionary Update()
  • Hack
  • sdk
  • python automation
  • main
  • reduce
  • typing
  • ord
  • print
  • network
  • matplotlib inline
  • Pickling
  • datastructure
  • bubble sort
  • find a node
  • Flask
  • calling function
  • tuple
  • GroupBy method
  • Pythonbrew
  • Np.Arange()
  • Modulo Operator
  • Python Or Operator
  • Keygen
  • cloud
  • pyautogui
  • python main
  • reduce function
  • type hints
  • python ord
  • format
  • python socket
  • jupyter
  • Unpickling
  • array
  • sorting
  • reversal
  • Python salaries
  • list sort
  • Pip
  • .groupby()
  • pyenv global
  • NumPy arrays
  • Modulo
  • OpenCV
  • Torrent
  • data
  • int function
  • file conversion
  • calculus
  • python typing
  • encryption
  • strings
  • big o calculator
  • gamin
  • HTML
  • list
  • insertion sort
  • in place reversal
  • learn python
  • String
  • python packages
  • FastAPI
  • argparse
  • zeros() function
  • AWS Lambda
  • Scikit Learn
  • Free
  • classes
  • turtle
  • convert file
  • abs()
  • python do while
  • set operations
  • data visualization
  • efficient coding
  • data analysis
  • HTML Parser
  • circular queue
  • effiiciency
  • Learning
  • windows
  • reverse
  • Python IDE
  • python maps
  • dataframes
  • Num Py Zeros
  • Python Lists
  • Fprintf
  • Version
  • immutable
  • python turtle
  • pandoc
  • semantic kernel
  • do while
  • set
  • tabulate
  • optimize code
  • object oriented
  • HTML Extraction
  • head
  • selection sort
  • Programming
  • install python on windows
  • reverse string
  • python Code Editors
  • Pytest
  • pandas.reset_index
  • NumPy
  • Infinite Numbers in Python
  • Python Readlines()
  • Trial
  • youtube
  • interactive
  • deep
  • kernel
  • while loop
  • union
  • tutorials
  • audio
  • github
  • Parsing
  • tail
  • merge sort
  • Programming language
  • remove python
  • concatenate string
  • Code Editors
  • unittest
  • reset_index()
  • Train Test Split
  • Local Testing Server
  • Python Input
  • Studio
  • excel
  • sgd
  • deeplearning
  • pandas
  • class python
  • intersection
  • logic
  • pydub
  • git
  • Scrapping
  • priority queue
  • quick sort
  • web development
  • uninstall python
  • python string
  • code interface
  • PyUnit
  • round numbers
  • train_test_split()
  • Flask module
  • Software
  • FL
  • llm
  • data science
  • testing
  • pathlib
  • oop
  • gui
  • visualization
  • audio edit
  • requests
  • stack
  • min heap
  • Linked List
  • machine learning
  • scripts
  • compare string
  • time delay
  • PythonZip
  • pandas dataframes
  • arange() method
  • SQLAlchemy
  • Activator
  • Music
  • AI
  • ML
  • import
  • file
  • jinja
  • pysimplegui
  • notebook
  • decouple
  • queue
  • heapify
  • Singly Linked List
  • intro
  • python scripts
  • learning python
  • python bugs
  • ZipFunction
  • plus equals
  • np.linspace
  • SQLAlchemy advance
  • Download
  • No
  • nlp
  • machiine learning
  • dask
  • file management
  • jinja2
  • ui
  • tdqm
  • configuration
  • Python is a beautiful language.