Python File Handling
Python provides a wide range of built-in functions for file handling. It makes it really easy to create, update, read, and delete files.
Open a File
When you want to work with a file, the first thing to do is to open it. You can open a file using open() built-in function specifying its name.
f = open('myfile.txt')
When you specify the filename only, it is assumed that the file is located in the same folder as Python. If it is somewhere else, you can specify the exact path where the file is located.
f = open(r'C:Python33Scriptsmyfile.txt')
Remember! While specifying the exact path, characters prefaced by (like n r t etc.) are interpreted as special characters. You can escape them using:
- raw strings like
- double backslashes like
Specify File Mode
Here are five different modes you can use to open the file:
|‘r’||Read (default)||Open a file for read only|
|‘w’||Write||Open a file for write only (overwrite)|
|‘a’||Append||Open a file for write only (append)|
|‘r+’||Read+Write||open a file for both reading and writing|
|‘x’||Create||Create a new file|
You can also specify how the file should be handled.
|‘t’||Text (default)||Read and write strings from and to the file.|
|‘b’||Binary||Read and write bytes objects from and to the file.This mode is used for all files that don’t contain text (e.g. images).|
Here are some examples:
# Open a file for reading f = open('myfile.txt') # Open a file for writing f = open('myfile.txt', 'w') # Open a file for reading and writing f = open('myfile.txt', 'r+') # Open a binary file for reading f = open('myfile.txt', 'rb')
Because read mode ‘r’ and text mode ‘t’ are default modes, you do not need to specify them.
Read a File
Suppose you have the following file.
First line of the file. Second line of the file. Third line of the file.
To read its contents, you can use
# Read entire file f = open('myfile.txt') print(f.read()) # Prints: # First line of the file. # Second line of the file. # Third line of the file.
By default, the
read() method reads the entire file. However, you can specify the maximum number of characters to read.
f = open('myfile.txt') print(f.read(3)) # Prints Fir print(f.read(5)) # Prints First
To read a single line from the file, use
f = open('myfile.txt') print(f.readline()) # Prints First line of the file. # Call it again to read next line print(f.readline()) # Prints Second line of the file.
You can loop through an entire file line-by-line using a simple for loop.
f = open('myfile.txt') for line in f: print(line) # Prints: # First line of the file. # Second line of the file. # Third line of the file.
If you want to read all the lines in a file into a list of strings, use
# Read all the lines in a file into a list of strings f = open('myfile.txt') print(f.readlines()) # Prints: # ['First line of the file.n', 'Second line of the file.n', 'Third line of the file.']
list(f) gives the same result as
Write a File
write() built-in method to write to an existing file. Remember that you need to open the file in one of the writing modes (‘w’, ‘a’ or ‘r+’) first.
Write mode ‘w’
In ‘w’ mode the entire contents of the file are overwritten:
f = open('myfile.txt', 'w') f.write('Overwrite existing data.')
Overwrite existing data.
Append mode ‘a’
In ‘a’ mode, text is added to the end of the file:
f = open('myfile.txt', 'a') f.write(' Append this text.')
First line of the file. Second line of the file. Third line of the file. Append this text.
Read-write mode ‘r+’
In ‘r+’ mode, the file is partially overwritten:
f = open('myfile.txt', 'r+') f.write('---Overwrite content---')
---Overwrite content--- Second line of the file. Third line of the file.
Write Multiple Lines
To write multiple lines to a file at once, use
writelines() method. This method accepts list of strings as an input.
f = open('myfile.txt', 'w') lines = ['New line 1n', 'New line 2n', 'New line 3'] f.writelines(lines)
New line 1 New line 2 New line 3
Flush Output Buffer
When you write to a file, the data is not immediately written to disk instead it is stored in buffer memory. It is written to disk only when you close the file or manually flush the output buffer.
# Flush output buffer to disk without closing f = open('myfile.txt', 'a') f.write('Append this text.') f.flush()
Close a File
It’s a good practice to close the file once you are finished with it. You don’t want an open file running around taking up resources!
close() function to close an open file.
f = open('myfile.txt') f.close() # check closed status print(f.closed) # Prints True
There are two approaches to ensure that a file is closed properly, even in cases of error.
The first approach is to use the with keyword, which Python recommends, as it automatically takes care of closing the file once it leaves the
with block (even in cases of error).
with open('myfile.txt') as f: print(f.read())
The second approach is to use the try-finally block:
f = open('myfile.txt') try: # File operations goes here finally: f.close()
Create a New File
If you try to open a file for writing that does not exist, Python will automatically create the file for you.
# Create a file and open it for writing # write mode f = open('newFile.txt', 'w') # append mode f = open('newFile.txt', 'a') # read + write mode f = open('newFile.txt', 'r+')
There is another way to create a file. You can use
open() method and specify exclusive creation mode ‘x’.
# Create a file exclusively f = open('newFile.txt', 'x')
Note that when you use this mode, make sure that the file is not already present, if it is, Python will raise the error.
Delete a File
You can delete a file by importing the OS module and using its
import os os.remove('myfile.txt')
Check if File Exists
If you try to open or delete a file that does not exist, an error occurs. To avoid this, you can precheck if the file already exists by using the
isfile() method from the OS module.
import os if os.path.isfile('myfile.txt'): f = open('myfile.txt') else: print('The file does not exist.')
Sometimes you need to read from the record in the middle of the file at that time you can use the
seek(offset, from_what) method. This function moves the file pointer from its current position to a specific position.
The offset is the number of characters from the from_what parameter which has three possible values:
- 0 – indicates the beginning of the file (default)
- 1 – indicates the current pointer position
- 2 – indicates the end of the file
Suppose you have the following file.
Here’s how you can use the
seek() method to randomly access the contents of a file.
f = open('myfile.txt', 'rb+') # go to the 7th character and read one character f.seek(6) print(f.read(1)) # Prints G # go to the 3rd character from current position (letter G) f.seek(3, 1) print(f.read(1)) # Prints K # go to the 3rd character before the end f.seek(-3, 2) print(f.read(1)) # Prints X
For Python 3.2 and up, in text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed.
In order to use seek from current position and end, you have to open the text file in binary mode.
If you want to check the current position of the file pointer, use the
f = open('myfile.txt') # initial position print(f.tell()) # Prints 0 # after reading 5 characters f.read(5) print(f.tell()) # Prints 5
Python Example for Beginners
Two Machine Learning Fields
There are two sides to machine learning:
- Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
- Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes
Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!
Latest end-to-end Learn by Coding Recipes in Project-Based Learning:
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.