- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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
Like other languages, Python provides some inbuilt functions for reading, writing, or accessing files. Python can handle mainly two types of files. The normal text file and the binary files.
For the text files, each lines are terminated with a special character '\n' (It is known as EOL or End Of Line). For the Binary file, there is no line ending character. It saves the data after converting the content into bit stream.
In this section we will discuss about the text files.
File Accessing Modes
|Sr.No||Modes & Description|
It is Read Only mode. It opens the text file for reading. When the file is not present, it raises I/O Error.
This mode for Reading and Writing. When the file is not present, it will raise I/O Error.
It is for write only jobs. When file is not present, it will create a file first, then start writing, when the file is present, it will remove the contents of that file, and start writing from beginning.
It is Write and Read mode. When file is not present, it can create the file, or when the file is present, the data will be overwritten.
This is append mode. So it writes data at the end of a file.
Append and Read mode. It can append data as well as read the data.
Now see how a file can be written using writelines() and write() method.
#Create an empty file and write some lines line1 = 'This is first line. \n' lines = ['This is another line to store into file.\n', 'The Third Line for the file.\n', 'Another line... !@#$%^&*()_+.\n', 'End Line'] #open the file as write mode my_file = open('file_read_write.txt', 'w') my_file.write(line1) my_file.writelines(lines) #Write multiple lines my_file.close() print('Writing Complete')
After writing the lines, we are appending some lines into the file.
#program to append some lines line1 = '\n\nThis is a new line. This line will be appended. \n' #open the file as append mode my_file = open('file_read_write.txt', 'a') my_file.write(line1) my_file.close() print('Appending Done')
At last, we will see how to read the file content from the read() and readline() method. We can provide some integer number 'n' to get first 'n' characters.
#program to read from file #open the file as read mode my_file = open('file_read_write.txt', 'r') print('Show the full content:') print(my_file.read()) #Show first two lines my_file.seek(0) print('First two lines:') print(my_file.readline(), end = '') print(my_file.readline(), end = '') #Show upto 25 characters my_file.seek(0) print('\n\nFirst 25 characters:') print(my_file.read(25), end = '') my_file.close()
Show the full content: This is first line. This is another line to store into file. The Third Line for the file. Another line... !@#$%^&*()_+. End Line This is a new line. This line will be appended. First two lines: This is first line. This is another line to store into file. First 25 characters: This is first line. This
- 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
- Writing files in background in Python
- Writing files in the background in Python
- Reading and writing binary file in C/C++
- What are reading and writing characters in C language?
- Reading/Writing a MS Word file in PHP
- How to open an Excel file with PHPExcel for both reading and writing?
- How to read text files using LINECACHE in Python
- Reading a Text file in java
- What are the text files and binary files in C language?
- How to read multiple text files from a folder in Python?(Tkinter)