mkaz.blog

Working with Python

Working with Files

Reading files

Here are several common ways to read files in Python. I use the with method to work with files because it will automatically close the file pointer after use.

Read entire file to list of lines

Use .readlines() to read the entire file in to a list, each line as an element in the list. Note, this will include the newlines at the end of each line.

with open('file.txt') as f:
    lines = f.readlines()

If you want list without the new lines:

with open('file.txt') as f:
    lines = [x.rstrip() for x in f]

Read file in by line

The file handler is also iterable, so you can loop over each line as process:

with open('file.txt') as f:
    for line in f:
        line = line.rstrip() # remove newline
        # ...

Read file in to single variable

You can read the entire file in to a single variable .

with open('file.txt') as f:
    content: f.read()

Write file

To write text to a file in Python, this will create a new file or overwrite an existing file:

content = "Text to write to file"
filename = "file.txt"
with open(filename, 'w') as f:
    f.write(content)

Append text to existing file

To open a file to append text use the a flag:

content = "Append text to file"
filename = "file.txt"
with open(filename, 'a') as f:
    f.write(content)

File exists

To check if file exists, use the pathlib module:

from pathlib import Path
 
p = Path("/path/to/file")
if p.is_file():
    print("Yes, file exists")

Find all files matching extension

Use pathlib.Path.glob()to search for files matching an extension. For example, to find all files matching .md extension in a directory:

pages = Path("/path/to/dir").glob("*.md")

Note: using just *.md will only search the directory specified, to recursively search all subdirectories use **/*.md:

pages = Path("/path/to/dir").glob("**/*.md")

Copy files and directories

Use shutil.copy2() to copy a file on your filesystem, if you want fine grain control over file system metadata look at shutil docs for other options.

shutil.copy2(source, destination)

Use shutil.copytree() to copy a directory tree.

shutil.copytree(source, destination)

Resources