

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reading and Writing to text files in Python Program
In this tutorial, we are going to learn about file handling in Python. We can easily edit files in Python using the built-in functions.
We have two types of files that can edit in Python. Let's see what they are.
Text Files
Text files are normal files that contain the English alphabets. We call the content present in the files as text.
Binary Files
Binary files contain data in 0's and 1's. We can't understand that language.
File Accessing Modes
Whenever we are working with the files in Python, we have to mention the accessing mode of the file. For example, if you want to open a file to write something in it, then it's a type of mode. Like the same way, we have different accessing modes.
Read Only - r
In this mode, we can only read the contents of the file. If the file doesn't exist, then we will get an error.
Read and Write - r+
In this mode, we can read the contents of the file, and we can also write data to the file. If the file doesn't exist, then we will get an error.
Write Only - w
In this mode, we can write content to the file. Data present in the file will be overridden. If the file doesn't exist, then it will create a new file.
Append Only - a
In this mode, we can append data to the file at the end. If the file doesn't exist, then it will create a new file.
Append and Write - a+
In this mode, we can append and write data to the file. If the file doesn't exist, then it will create a new file.
Writing to a File
Let's see how to write data to a file.
Open a file using the open() in w mode. If you have to read and write data using a file, then open it in an r+ mode.
Write the data to the file using write() or writelines() method
Close the file.
We have the following code to achieve our goal.
Example
# opening a file in 'w' file = open('sample.txt', 'w') # write() - it used to write direct text to the file # writelines() - it used to write multiple lines or strings at a time, it takes ite rator as an argument # writing data using the write() method file.write("I am a Python programmer.\nI am happy.") # closing the file file.close()
Go the directory of the program, and you will find a file named sample.txt. See the content in it.
Reading from a File
We have seen a method to write data to a file. Let's examine how to read the data which we have written to the file.
Open a file using the open() in r mode. If you have to read and write data using a file, then open it in an r+ mode.
Read data from the file using read() or readline() or readlines() methods. Store the data in a variable.
Display the data.
Close the file.
We have the following code to achieve our goal.
Example
# opening a file in 'r' file = open('sample.txt', 'r') # read() - it used to all content from a file # readline() - it used to read number of lines we want, it takes one argument which is number of lines # readlines() - it used to read all the lines from a file, it returns a list # reading data from the file using read() method data = file.read() # printing the data print(data) # closing the file file.close()
Output
If you run the above program, you will get the following results.
I am a Python programmer. I am happy.
Conclusion
I hope you understand the tutorial. If you have any doubts, mention them in the comment section.
- Related Questions & Answers
- Reading and Writing to text files in Python
- Reading and Writing to text files in C#
- Reading and Writing Files in Python
- Reading and Writing Files in Perl
- Reading and writing Excel files using the openpyxl module in Python
- Reading and Writing CSV File using Python
- Reading and writing binary file in C/C++
- Writing files in background in Python
- What are reading and writing characters in C language?
- Writing files in the background in Python
- Reading/Writing a MS Word file in PHP
- How to open an Excel file with PHPExcel for both reading and writing?
- Reading a Text file in java
- How to read text files using LINECACHE in Python
- What are the text files and binary files in C language?