Python 3

home

Introduction to Python

davidbpython.com




File and Directory Listings


writing to files using the file object

To open a file for writing, use the 2nd argument 'w'.


fh = open('new_file.txt', 'w')
fh.write("here's a line of text\n")
fh.write('I add the newlines explicitly if I want to write to the file\n')
fh.close()






appending to files using the file object

To open a file for appending, use the 2nd argument 'a'.


fh = open('new_file.txt', 'a')
fh.write("20250505 1203   something happened\n")
fh.close()






use os.getcwd() to show the present/current working directory.

Below program assumes were are starting in our home directory:

import os                # os ('operating system') module talks
                         # to the os (for file access & more)

cwd = os.getcwd()        # str, '/Users/david'

print(cwd)






a sample file tree

We'll use this tree to explore relative filepaths.


dir1
├── file1.txt
├── test1.py
│
├── dir2a
│   ├── file2a.txt
│   ├── test2a.py
│   │
│   ├── dir3a
│   │   ├── file3a.txt
│   │   ├── test3a.py
│   │   │
│   │   └── dir4
│   │       ├── file4.txt
│   │       └── test4.py
└── dir2b
    ├── file2b.txt
    ├── test2b.py
    │
    └── dir3b
       ├── file3b.txt
       └── test3b.py






relative filepaths

These paths locate files relative to the present working directory.


If the file you want to open is in the same directory as the script you're executing, use the filename alone:

fh = open('filename.txt')





relative filepaths: parent directory

To reach the parent directory, prepend the filename with ../


fh = open('../filename.txt')





relative filepaths: child directory

To reach the child directory, prepend the filename with the name of the child directory.


fh = open('childdir/filename.txt')





relative filepaths: sibling directory

To reach a sibling directory, prepend the filename with ../ and the name of the child directory.


fh = open('../childdir/filename.txt')

To reach a sibling directory, we must go "up, then down" by using ../ to go to the parent, then the sibling directory name to go down to the child.






absolute filepaths

These paths locate files from the root of the filesystem.


In Windows, absolute paths begin with a drive letter, usually C:\:

""" test3a.py:  open and read a file """

filepath = r'C:\Users\david\Desktop\python_data\dir1\file1.txt'
fh = open(filepath)

print(fh.read())

(Note that r'' should be used when expressing in our Python program any Windows paths that contain backslashes.)


On the Mac, absolute paths begin with a forward slash:

""" test3a.py:  open and read a file """

filepath = '/Users/david/Desktop/python_data/dir1/file1.txt'
fh = open(filepath)

print(fh.read())

(The above paths assume that the python_data folder is in the Desktop directory; your may have placed yours elsewhere on your system. Of course, the above paths also assume that my home directory is called david/; yours is likely different.)






os.path.join()

This function joins together directory and file strings with slashes appropriate to the current operating system.


dirname = '/Users/david'
filename = 'journal.txt'

filepath = os.path.join(dirname, filename)             # '/Users/david/journal.txt'

filepath2 = os.path.join(dirname, 'backup', filename)  # '/Users/david/backup/journal.txt'






os.listdir(): list a directory

os.listdir() can read the contents of any directory.


import os

mydirectory = '/Users/david'

items = os.listdir(mydirectory)

for item in items:                                # 'photos'

    item_path = os.path.join(mydirectory, item)

    print(item_path)  # /Users/david/photos/
                      # /Users/david/backups/
                      # /Users/david/college_letter.docx
                      # /Users/david/notes.txt
                      # /Users/david/finances.xlsx

Note the os.path.join() call. This is a standard algorithm for looping through a directory -- each item must be joined to the directory to ensure that the filepath is correct.






exceptions for missing or incorrect files or directories

Several exceptions can indicate a file or directory misfire.


exception typetriggered by
FileNotFoundErrorattempt to open a file not in this location
FileExistsErrorattempt to create a directory (or in some cases a file) that already exists
IsADirectoryErrorattempt to open() a file that is already a directory
NotADirectoryErrorattempt to os.listdir() a directory that is not a directory
PermissionErrorattempt to read or write a file or directory to which you haven't the permissions
WindowsError, OSErrorthese exception types are sometimes raised in place of one or more of the above when on a Windows computer







traversing a directory tree with os.walk()

os.walk() visits every directory in a directory tree so we can list files and folders.


import os
root_dir = '/Users/david'
for root, dirs, files in os.walk(root_dir):

    for tdir in dirs:                    # loop through dirs in this directory
        print(os.path.join(root, tdir))  # print full path to tdir

    for tfile in files:                  # loop through files in this dir
        print(os.path.join(root, tfile)) # print full path to file


At each iteration, these three variables are assigned these values:





[pr]