HOME/Articles/

python get files in directory

Article Outline

How do you get the filenames in a directory? You can do that automatically with <a href="https://python.org/">Python</a>, including all files in sub directories.

To get started, you need to import the os module, this contains operating system specific functionality.

In Python, you can use OS.walk(). OS.walk() gets the filenames in a directory. It does so by walking the <a href="https://pythonspot.com/python-tree/">tree</a> either top-down or bottom-up.

You should know the <a href="https://pythonbasics.org">basics of Python</a> or this will be a bit confusing.

OS.walk()

Calling the OS.walk() method returns a 3-<a href="https://pythonspot.com/python-tuples/">tuple</a> (dirpath, dirnames, filenames).

os.walk(top, topdown=True, onerror=None, followlinks=False)
  • dirpath is a string, the path to the directory.
  • topdown is an optional argument
  • onerror is an optional argument, by default errors are ignored
  • followlinks is false will not walk down into symbolic links that resolve to directories

The function returns lists for dirnames and filesnames, so you can use a <a href="https://pythonbasics.org/for-loops/">for loop</a> to parse it.

To output the files in a directory you can run the code below:

import os

for root, dirs, files in os.walk("/etc/"):
    for name in files:
        print( root + "/" + name)

This outputs all files in the directory, including sub-directories. To get all sub directories you can just change files to dirs:

import os

for root, dirs, files in os.walk("/etc/"):
    for name in dirs:
        print( root + "/" + name)

You can get the file size too:

import os
from os.path import join, getsize
from pathlib import Path

# needs Python 3.5+
home = str(Path.home())

for root, dirs, files in os.walk(home + "/Desktop/"):
    print(root, "consumes", end=" ")
    print(sum(getsize(join(root, name)) for name in files), end=" ")
    print("bytes in", len(files), "non-directory files")

I've used this line to get the home directory in Python 3.5:

from pathlib import Path
home = str(Path.home())

If you have an older Python version you can use

from os.path import expanduser
home = expanduser("~")

Related links:

  • <a href="https://docs.python.org/3/library/os.html">Python os module</a>
  • <a href="https://pythonbasics.org/">Learn Python</a>