Skip to main content
Engineering LibreTexts

14.4: Filenames and paths

  • Page ID
    40811
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Files are organized into directories (also called “folders”). Every running program has a “current directory”, which is the default directory for most operations. For example, when you open a file for reading, Python looks for it in the current directory.

    The os module provides functions for working with files and directories (“os” stands for “operating system”). os.getcwd returns the name of the current directory:

    >>> import os
    >>> cwd = os.getcwd()
    >>> cwd
    '/home/dinsdale'
    

    cwd stands for “current working directory”. The result in this example is /home/dinsdale, which is the home directory of a user named dinsdale.

    A string like '/home/dinsdale' that identifies a file or directory is called a path.

    A simple filename, like memo.txt is also considered a path, but it is a relative path because it relates to the current directory. If the current directory is /home/dinsdale, the filename memo.txt would refer to /home/dinsdale/memo.txt.

    A path that begins with / does not depend on the current directory; it is called an absolute path. To find the absolute path to a file, you can use os.path.abspath:

    >>> os.path.abspath('memo.txt')
    '/home/dinsdale/memo.txt'
    

    os.path provides other functions for working with filenames and paths. For example, os.path.exists checks whether a file or directory exists:

    >>> os.path.exists('memo.txt')
    True
    

    If it exists, os.path.isdir checks whether it’s a directory:

    >>> os.path.isdir('memo.txt')
    False
    >>> os.path.isdir('/home/dinsdale')
    True
    

    Similarly, os.path.isfile checks whether it’s a file.

    os.listdir returns a list of the files (and other directories) in the given directory:

    >>> os.listdir(cwd)
    ['music', 'photos', 'memo.txt']
    

    To demonstrate these functions, the following example “walks” through a directory, prints the names of all the files, and calls itself recursively on all the directories.

    def walk(dirname):
        for name in os.listdir(dirname):
            path = os.path.join(dirname, name)
    
            if os.path.isfile(path):
                print(path)
            else:
                walk(path)
    

    os.path.join takes a directory and a file name and joins them into a complete path.

    The os module provides a function called walk that is similar to this one but more versatile. As an exercise, read the documentation and use it to print the names of the files in a given directory and its subdirectories. You can download my solution from http://thinkpython2.com/code/walk.py.


    This page titled 14.4: Filenames and paths is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?