Python append to file

You can append file with Python. You need to open() file in append mode by passing a as second argument. Use write() function to append new content.

This notebook was created with MLJAR Studio

MLJAR Studio is Python code editior with interactive code recipes and local AI assistant.
You have code recipes UI displayed at the top of code cells.

Documentation

Let's use file todo.txt in this notebook. The current file content is:

# TODO

- [ ] clean desk
- [ ] water flowers
- [ ] check github issues

Let's append new task in the list.

# open file in append mode
with open(r"/home/piotr/documents/todo.txt", "a") as fin:
    fin.write("- [ ] make Python easy")

Summary

The file todo.txt after running above code:

# TODO

- [ ] clean desk
- [ ] water flowers
- [ ] check github issues
- [ ] make Python easy

We need exactly this. I need to say that making Python easy is possible. It is time consuming, because I would like to provide many Python notebooks with example use cases. Fingers crossed! ๐Ÿ˜Š

Recipes used in the python-append-file.ipynb

All code recipes used in this notebook are listed below. You can click them to check their documentation.

Packages used in the python-append-file.ipynb

List of packages that need to be installed in your Python environment to run this notebook. Please note that MLJAR Studio automatically installs and imports required modules for you.