In this tutorial, we’ll learn how to properly write file in Python. We’ll going through multiple examples to understand how to write file in Python language.
- Introduction: Write File In Python
- Python File Write Method
- Example 1: Write To A Non-Existing File In Python
- Example 2: Write To An Existing File In Python
Introduction: Write File In Python
Files are used to store data. In our previous article, we’ve learned how to read a Python file. Click here if you want to learn it in detail. In this post, we’ll learn how to write file in Python.
We’ll learn it in detail using multiple Python code examples. So let’s not wait anymore and just jump right into it.
Python File Write Method
We’ll be using the write() method of file for writing content to it. The syntax is shown below:
- Python file write method takes one string argument and its mandatory.
- This argument specifies the new content to be added to the file.
- If the file is specified but it doesn’t exist then a new file will be created with the same name and it’ll store the new data.
- Write method overrides the existing data with new data. So be careful while using this method.
Let’s now practically learn how to properly write file in Python.
Example 1: Write To A Non-Existing File In Python
In this example, we won’t create any file but will specify a file name and write some data to it. See below code:
file = open('data.text', 'w') file.write('This is Python') file.close()
- We’ve specified the second argument of open function. By default, its set to read(‘r’). In order to set it to write, we’ve used the character(‘w’) within quotes. We also have stored the file inside a variable. You can give it a name of your choice.
- We then have used the Python file write method and inside its string parameter, we’ve specified a simple text.
- Its a very good practice to always close the file after the work is done using its close method.
If you run it then you will see that a new text file will be created with the same name and data. In our case, the file is also created. Content of that file is given below:
This is Python
We can also use try finally block. Its a good practice. See below code:
try: file = open('data.text', 'w') file.write('This is Python') finally: file.close()
We can use the with syntax to specify the same code in few lines. See below code:
with open('data.text', 'w') as file: file.write('This is Python')
Example 2: Write To An Existing File In Python
We’ll be using the same above example. Let’s now add a new data to the same file. See below code:
file = open('data.text', 'w') file.write('New content has been added') file.close()
Old File Content
This is Python
New File Content
New content has been added
So this is how we can write file in Python. I’d be glad to see you share this post with your developer friends.
Don’t hesitate to ask if you still have any questions regarding how to write file in Python. I’ll be very happy to solve all your queries.
To conclude this tutorial, hope you now have a detailed practical knowledge of how to properly write file in Python. I’ll be very happy to receive your feedback on this post.
Do visit my other articles on Python programming. Links to some of them are listed below. Others can be found using the navigation bar or search box of this site. Thank you for reading this post.
You may like to read:
[Solved] How To Easily Read File In Python
How To Use Python String Format_Map Method [Python Easy Guide]
[3 Ways] How To Easily Format String In Python
[Solved] How To Convert Python Tuple To String
How To Easily Get Input In Python [Python Code Examples]
How To Easily Reverse String In Python [Python Code Examples]