Jump to content.

Python reference sheet (work in progress)

What standard library functions to use to do some basic tasks in Python, and how to invoke them. The reference version being Python 3.5 for our purposes; 2.7 differs in certain respects, but if you’re still using that then you should be migrating already.

Filesystem access

Main modules: io, os (including os.path), shutil, glob. The io.open function is available from the builtins namespace. The os.open function is low-level and not what you’re after (equivalent to C open, while io.open is vaguely equivalent to C fopen).

New folder, copy, move, rename, delete

Create a folder:

os.mkdir("hello") # Must not exist, containing folder must exist.
os.makedirs("hello") # Must not exist, containing folder may or may not exist.
os.makedirs("hello", exist_ok=True) # May exist, containing folder may or may not exist.

Copy a file or folder:

shutil.copy("bob.txt", "cat.txt") # File
shutil.copytree("bob", "cat") # Folder

Move a file or folder, either within or between filesystems:

shutil.move("hello.txt", os.path.join("cat", "hello"))

Rename a file or folder, or move it within a single filesystem:

os.rename("bob", "cat")
os.rename("hello.txt", os.path.join("cat", "hello.txt"))

Delete a file or empty folder:

os.unlink("hello.txt") # Or os.remove (does exactly the same thing)
os.rmdir("bob") # Must be empty
shutil.rmtree("bob") # May or may not be empty

Delete an empty folder along with any otherwise-empty containing folders. This is less likely to be the one you’re after and is listed mostly for completeness:

os.removedirs("bob")

Creating, reading and writing files

Create a file containing text (will create an initially empty file to write to and replace a file if it exists):

f = open("hello.txt", "w", encoding="utf-8")
f.write("¡Hello, world!")
f.close()

Append text to an existing file:

f = open("hello.txt", "a", encoding="utf-8")
f.write("\n¡Bonjour, tout le monde!") # Converts \n based on platform
f.close()

Read the text contents of a file to a string:

f = open("hello.txt", "r", encoding="utf-8")
b = f.read() # Converts \r\n and \r to \n
f.close()

Read the text contents of a non-UTF8 file to a string:

# Western European (Windows)
f = open("hello.txt", "r", encoding="windows-1252")
b = f.read()
f.close()

# Japanese (Shift-JIS) in Windows variant
f = open("nihongo.txt", "r", encoding="ms-kanji")
b = f.read()
f.close()

Read the contents of a non-text file:

f = open("hello.db", "rb")
b = f.read() # Gives a bytes object, does not convert newlines.
f.close()

Write to a non-text file:

f = open("hello.db", "wb")
f.write(b"\x00\x04\n\r\r\n\n\x1AHello\xFAWorld!!!")
f.close()

Handling folders and probing files

Get current (and parent) folder path:

cwd = os.getcwd() # Current
parent = os.path.split(os.getcwd())[0]

# Alternatives using abspath:
cwd = os.path.abspath(os.curdir) # or os.path.curdir (usually ".")
parent = os.path.abspath(os.pardir) # usually ".."

Change directory:

os.chdir("bob")

Check if a file or folder exists, and whether somethings a file or folder:

if os.path.exists("bob.txt"):
    # do stuff
if os.path.isdir("bob.txt"):
    # do stuff (exists and is a folder)
if os.path.isfile("bob.txt"):
    # do stuff (exists and is a file)

List files and folders within a folder:

foo = os.listdir("bananas")
bar = os.listdir("bob")

Step through all files and folders under a directory:

for root, dirs, files in os.walk("bob"):
    for filename in files:
        filepath = os.path.join(root, filename)
        # ...
    for foldername in dirs:
        dirpath = os.path.join(root, foldername)
        # ...

Step through all files and folders matching a filename wildcard pattern:

for path in glob.glob("b?bcats/*.txt"):
    # ...

Step through all files and folders matching a recursive filename wildcard pattern (i.e. matching subdirectories recursively):

for path in glob.glob("b?bcats/**/*.txt", recursive=True):
    # ...

Read file size in bytes:

size = os.stat("hello.txt").st_size

Read file timestamps (in UNIX time):

atime = os.stat("hello.txt").st_atime # Access timestamp
mtime = os.stat("hello.txt").st_mtime # Modification timestamp
# INODE change timestamp on Linux, creation timestamp on Windows:
ctime = os.stat("hello.txt").st_ctime

Write file timestamps (in UNIX time):

os.utime("hello.txt", (atime, mtime))
os.utime("hello.txt") # "Touches" file (set timestamp to current time).