It is pretty standard that large chunks of data need to store in the Files. Python is widely used in data analytics and comes with some inbuilt functions to write data into files.
We can open a file and do different operations on it, such as write new contents into it or modify a file for appending content at the end of a file.
After reading this tutorial, you’ll learn: –
- Writing into both text and binary files
- The different modes for writing a file
- Writing single or multiple lines in a file.
- All methods for writing a file such as
write()andwriteline(). - Appending new contents at the end of an existing file
- Open file for both reading and writing.
Table of contents
Access Modes for Writing a file
Access mode specifying the purpose of opening a file.
Whenever we need to write text into a file, we have to open the file in one of the specified access modes. We can open the file basically to read, write or append and sometimes to do multiple operations on a single file.
To write the contents into a file, we have to open the file in write mode. Open a file using the built-in function called open(). This function takes two parameters, namely filename, and access mode, and returns the file pointer.
We can open a file for modifying or overwrite its contents by using any one of the modes described in the following table.
| Access Mode | Description |
|---|---|
w | Open a file for writing. In the case of an existing file, it truncates the existing content and places the filehandle at the beginning of the file. A new file is created if the mentioned file doesn’t exist. |
w+ | Open a file for both reading and writing. In the case of the existing file, it will truncate the entire content and place the filehandle at the beginning of the file. |
wb | Open a binary file for writing. The FileHandle is placed at the beginning of the file, and existing content will be truncated. A new file is created otherwise. |
a | Opens a file for writing. The FileHandle will be placed at the end of the file. In the case of the existing file, the new content will be added after the existing content. A new file will be created otherwise. |
a+ | Open a file for appending as well as reading. The FileHandle is placed at the end of the file. In the case of the existing file, the new content will get added after the existing content. A new file is created otherwise. |
Steps for Writing Data into a File in Python
To write into a file, Please follow these steps:
- Find the path of a file
We can read a file using both relative path and absolute path. The path is the location of the file on the disk.
An absolute path contains the complete directory list required to locate the file.
A relative path contains the current directory and then the file name. - Open file in write mode
Pass file path and access mode
wto theopen()function. The access mode opens a file in write mode.
For example,fp= open(r'File_Path', 'w') - Write content into the file
Once a file is opened in the
writemode, write text or content into the file using the write() method.
For example,fp.write('new text').
Thewrite()method will add new text at the beginning of a file. For an existing file, this new content will replace the existing content. If the file is not already present a new file will be created, and content is written into it. - Close file after completing the write operation
We need to make sure that the file will be closed properly after completing the file operation. Use
fp.close()to close a file. - Append content at the end of the file
Pass file path and access mode
ato theopen()function to open a file in append mode.
For example,fp= open(r'File_Path', 'a')
Next, use thewrite()method to write content at the end of the file without deleting the existing content
Example: Write to a Text file in Python
The following code shows how to write a string into a new file. In this example, we are writing a single line into a file.
Done Writing This is new content
In the above example, the write_demo.txt was not present, and therefore a new file is created with that name. here we used a relative path. The content is written at the beginning.