eolas/neuron/7aecb0c4-f9f9-4579-9564-8a4cb5e9c958/Writing_to_files_in_Python.md

33 lines
767 B
Markdown
Raw Normal View History

2024-12-09 18:34:15 +00:00
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# Writing to files in Python
We create a file object with `open()` and use the `write` method:
```py
# Open file in write mode
file = open("example.txt", "w")
# Write some text to the file
file.write("Hello, this is an example text written using Python.")
# Close the file
file.close()
```
Alternatively we use `with open` which automatically closes the file:
```py
with open("example.txt", "w") as file
file.write('some lines')
```
> Note that in the above example, if the file does not already exist, it will
> create it. If it does exist, it will overwrite its contents with the new data.
> So we use `write` to create new files as well as to write to existing files.