Working with Files & the Filesystem in Python: Reference and Examples

Last updated:

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

All examples assume Python 3, unless otherwise stated

Manipulating filesystem paths is a very common need in most if not all applications; most problems can be solved using either os.path or pathlib modules.

Pathlib is used in some examples here; it comes by default with Python 3 but you can also use it in Python 2 via Pip: pip install pathlib

Write string to file

write is just one method of class File. See all here

# will be overwritten if it already exists!
with open("filename.txt","w") as f:
    f.write("foo bar")

Read the whole file contents to a string

with open("filename.txt","r") as f:
    contents = f.read()

Read a file line-by-line, without loading it all to memory (buffered reading)

This is easy with python:

with open("path/to/file") as f:
    for line in f:
        # process the line

You can also use f.readline() to read a whole line (be careful if the file has no line breaks) or f.read(size), which takes an argument indicating the maximum number of bytes to be read into memory.

Test if a file exists

This method returns True if the given path exists and it's a file.

If you just want to test if a path exists (be it a file, directory or even link), use os.path.exists() instead.

import os.path

path_to_file = "/path/to/file"

if os.path.isfile(path_to_file):
    # it exists, and it is a regular
    # (i.e. not a directory) file

Relative to Absolute Path

Use os.path.abspath. But remember that the relative path is calculated with respect to your current working directory.

Note that this will not warn you if you supply a path that resolves to a file/dir that doesn't exist

If you want the relative path to be calculated with respect to the current file location, don't forget to prepend __file__+"/" to the relative path!

import os

# path is relative to the current WORKING directory!
abs_path = os.path.abspath("../../other-dir")

# path is relative to the directory where THIS file
# is located!
abs_path = os.path.abspath(__file__+"/"+"../../other-dir")

Get current working directory

The current working directory (or cwd) is the directory the user that first called the script was located.

import os

os.getcwd()

Get the directory where this (current) file is located

Note that this is not at all the same thing as the current working directory (seen above)

import os

# __file__ points to the where the current file is located
os.path.dirname(os.path.realpath(__file__))

List all files in a directory

import os

os.listdir(path_to_directory)

To list all files recursively under some path (i.e. including subdirectories):

import os

# you may omit trailing slashes
# i.e. '/tmp' works too
path = '/tmp/'

for root, directories, filenames in os.walk(path):
    for directory in directories:
        # in python 3 print needs parens
        print(os.path.join(root, directory))
    for filename in filenames:
        # in python 3 print needs parens
        print(os.path.join(root,filename))

Reading CSV files

import csv

with open("path/to/file") as f:
    reader = csv.reader(f)

    for line in reader:
        # line is an array of strings

You can provide extra options, too. If you have a line like this:

"16","foo bar, baz","foo, bar \"...\" baz...",   "foo,bar, baz"

You need to instruct the reader to disconsider commas within double quotes and to use '\' to escape double quotes when inside a double-quoted block. You also need to tell it to disregard empty spaces before doublw quotes:

with open("path/to/file",'r') as f:
    reader = csv.reader(f,quotechar='"', delimiter=',', escapechar='\\', skipinitialspace=True)

    for line in reader:
        # line is an array of strings

See also

Dialogue & Discussion