Skip to content
On this page

Read/Write Files

Text File

To read/write to a file in Python, you will want to use the with statement, which will close the file for you after you are done, managing the available resources for you.

Reading

python
with open('filename.txt') as file:
    content = file.read()

You can also iterate through the file line by line:

python
with open('filename.txt') as file:
    for line in file:
        print(line, end='')

Writing

python
with open('filename.txt', 'w') as file:
    file.write('Hello world!\n')

JSON

Reading

python
import json

with open("filename.json", "r") as file:
    content = json.load(file)

Writing

python
import json

content = {"name": "Joe", "age": 20}
with open("filename.json", "w") as file:
    json.dump(content, file, indent=4)

TOML

Reading

python
import toml

with open("filename.toml") as file:
    content = toml.load(file)

Writing

python
import toml

content = {"name": "Joe", "age": 20}
with open("filename.toml", "w") as file:
    toml.dump(content, file)

YAML

Reading

python
import yaml

with open("filename.yaml") as file:
    content = yaml.load(file, Loader=yaml.FullLoader)

Writing

python
import yaml

content = {"name": "Joe", "age": 20}
with open("filename.yaml", "w") as file:
    yaml.dump(content, file)

Released under the MIT License.