Open a File in Write Mode in Python
In Python, you can open a file in write mode using the open()
function with the mode parameter set to 'w'
. This allows writing new content to the file. If the file already exists, it will be overwritten. If the file does not exist, Python will create a new one.
Examples for Opening a File in Write Mode
1. Writing to a File Using 'w'
Mode
In this example, we open a file named sample.txt
in write mode and write a string to it.
In this example, we will consider a file example.txt, open it in write mode, and write some content to this file.
main.py
# Opening a file in write mode
file = open("sample.txt", "w")
# Writing data to the file
file.write("Hello, this is a test file.")
# Closing the file
file.close()
print("Data written successfully.")
Explanation:
- The
open()
function openssample.txt
in write mode ('w'
). - The
write()
method writes the given text to the file. - The
close()
method ensures that the file is properly closed.
Output:

The text "Hello, this is a test file."
is now stored inside sample.txt
.

2. Overwriting an Existing File
If the file already exists, opening it in write mode will erase its previous content.
We will consider the same sample.txt from the previous example, where we have already written some content to it.
main.py
# Opening the same file again in write mode
file = open("sample.txt", "w")
# Writing new content to overwrite previous data
file.write("This will overwrite the previous content.")
# Closing the file
file.close()
print("File overwritten successfully.")
Explanation:
- When the file is opened in
'w'
mode, its previous content is erased. - The
write()
method replaces the old content with the new text.
Output:

Now, sample.txt
contains "This will overwrite the previous content."
.

3. Writing Multiple Lines to a File
We can write multiple lines at once using the writelines()
method.
main.py
# Opening a file in write mode
file = open("sample.txt", "w")
# List of lines to be written
lines = ["First line\n", "Second line\n", "Third line\n"]
# Writing multiple lines to the file
file.writelines(lines)
# Closing the file
file.close()
print("Multiple lines written successfully.")
Explanation:
- The
writelines()
method writes multiple lines at once. - Each string in the list ends with
\n
to ensure new lines.
Output:

The sample.txt
file now contains:

4. Using with open()
for Automatic File Handling
The with
statement automatically closes the file after writing.
main.py
# Using with open() to write data
with open("sample.txt", "w") as file:
file.write("Using 'with' statement for writing files.")
print("Data written successfully using 'with' statement.")
Explanation:
- The
with
statement ensures that the file is automatically closed after writing. - This method is preferred as it handles file closing automatically.
Output:
Data written successfully using 'with' statement.
The sample.txt
file now contains:
Using 'with' statement for writing files.
Conclusion
Concluding this tutorial, opening a file in write mode ('w'
) in Python allows us to:
- Write new content to a file.
- Overwrite existing content if the file already exists.
- Write multiple lines using
writelines()
. - Use
with open()
for automatic file handling.
Using with open()
is recommended for better resource management.