![]() |
VOOZH | about |
Before writing to a file, let’s first understand the different ways to create one. Creating a file is the first step before writing data. In Python you control creation behaviour with the mode passed to open() (or with pathlib helpers).
| Mode / Option | Description |
|---|---|
| "w" | Write mode: creates file if missing, truncates (erases) if it exists |
| "a" | Append mode: creates file if missing, writes data always at the end |
| "x" | Exclusive create: creates new file, but fails with FileExistsError if it already exists |
| "b" | Binary flag: used with other modes (e.g., "wb", "ab") for binary files |
| "+" | Read/write flag: combine with other modes (e.g., "r+", "w+") for both reading and writing |
| encoding= | Specify text encoding (e.g., "utf-8") when working with text files |
| newline= | Control newline translation in text mode (e.g., "\n") |
Note: You can combine flags like "wb" (write + binary) or "a+" (append + read). Always pick a mode based on whether you want to overwrite, append, or strictly create a new file.
Opening a file in write mode ("w") clears any existing content before writing new data. This is useful when you want to start fresh and replace old information with new output.
Example: This code writes two lines to file.txt, overwriting any previous content.
Output
Created using write mode.
Second line.
Explanation: Here "w" truncates file.txt and writes fresh content. The with block ensures the file is properly saved and closed.
Using "a" mode (append), we can add new data at the end of a file without removing what’s already there.
Example: This writes an extra line to file.txt without removing existing content.
Output
Created using write mode.
Second line.
Appended line.
Explanation: "a" always writes at the end. Useful for logs, reports, or when you want to keep history intact.
With "x" mode, Python creates a new file but raises FileExistsError if the file already exists. This prevents accidental overwrites.
Example: This tries to create file.txt only if it doesn’t exist, otherwise prints an informative message.
Output
file.txt already exists, exclusive creation aborted.
Explanation: "x" mode is useful for safe file creation. It guarantees no overwriting if the file exists, Python raises a FileExistsError instead.
Sometimes you need to write several lines at once instead of one by one. Python provides two common approaches: writelines() for lists of strings and join() for building a single text block.
Example 1: This writes a list of lines; each element includes a \n.
Output
First line
Second line
Third line
Explanation: writelines() writes each string in the list directly. It does not add newlines automatically you must include \n in each string yourself.
Example 2: join + single write (often cleaner)
This creates the final text with '\n'.join(...) and writes once.
Output
Line A
Line B
Line C
Explanation: Building a single string and calling write() once can be more efficient and less error-prone, especially when you must ensure consistent newlines.
For non-text data (like images, audio, or other binary content), use binary write mode ("wb"). This treats the file as raw bytes instead of text.
Example: The following code writes a small sequence of bytes into a file called file.bin and then reads it back in binary mode.
Output
b'\x00\x01\x02\x03\x04'
Explanation:
pathlib provides an object-oriented way to work with files and paths. It is more modern but slightly advanced compared to open().
Hello World
Explanation: