Table of Contents
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
.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()
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
content = "Append text to file" filename = "file.txt" with open(filename, 'a') as f: f.write(content)
To check if file exists, use the
from pathlib import Path p = Path("/path/to/file") if p.is_file(): print("Yes, file exists")
Find all files matching extension
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
pages = Path("/path/to/dir").glob("**/*.md")
Copy files and directories
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
shutil.copytree() to copy a directory tree.
- Python Docs: Pathlib modules