Interview Kickstart has enabled over 21000 engineers to uplevel.
As a Software Engineer, you have to handle many files from multiple sources. You have to deal with files in different formats such as .xlsx, .json, .txt, etc. It is critical to understand how to work with these files.
File handling refers to storing data in the form of input or output produced by running programs in data files, like a text file or a binary file for future reference and analysis.
In this article, you will understand how to open files, write in them, read from them, etc. Here are some topics we’ll cover in this article:
A file is generally a collection of data, and a filename is what we use to identify it. A file can be a document, an image, an audio file, or a video file. We can save, store, delete, and manipulate a file. We can move the file from one location to another and transfer it from one network connection to another. Usually, we can identify a file with its extension.
Flat files represent files that contain records with no defined and structured relationship between records. These files only contain basic formatting and have a limited number of fields. Sometimes, these files may not have any format.
Here’s an example of such a flat file:
Further, there are two types of flat files:
Non-flat files represent files where each record has an index assigned. The location of a specific record or data point can be retrieved using these index values. An example of a non-flat file is as follows:
There is a defined relationship between records and an index value that we can use to identify each record or data.
Python has various built-in functions to help us read, write, manipulate, and perform operations on an accessible file. For example, the io module is a default module Python Software Engineers popularly use for accessing files.
Before you read, write, or perform any manipulations on a file, you must open it. To do this, you can use the following syntax:
open(file_name, access_mode)
This syntax returns a file object called handle. Once you generate this handle, you can use it to read from or write to a file.
In Python, the open() built-in function is used to open a file.
open(file, mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
closefd=True,
opener=None)
The open function returns a corresponding file object. It consists of the following arguments:
‘r’
Opens file for reading (default)
‘w’
Opens file for writing; truncates the file first
‘x’
Opens file for exclusive creation; fails if the file already exists.
‘a’
Opens file for writing; appends to the end of the file if it exists.
‘b’
Represents binary mode
‘t’
Represents text mode (default)
‘+’
Opens file for updating (reading and writing)
Note:
To open a sample.txt file, you can use the following code. The sample.txt file can contain any information or content.
Code:
filename = 'sample.txt'
access_mode = 'r'
with open(filename, access_mode) as file:
print(file.read())
file.close()
Result:
There are three ways you can read from a file. They are as follows:
Here, x represents the number of bytes you need to read. If you do not pass x, then the complete file is read.
You can create a .txt file that contains the following content:
Let us look at the three ways you can use to achieve this goal:
1. You can use the read() function.
Code:
with open(filename, access_mode) as file:
print(file.read())
file.close()
Result:
If you include a value x in read, you will observe a difference. Let’s consider x = 4.
Code:
with open(filename, access_mode) as file:
print(file.read(4))
file.close()
Result:
Note that it prints the first four characters. As you can observe, it gives at most n bytes of a file.
2. You can use the readline() function.
This function allows you to read only one line and at most n bytes.
Code:
with open(filename, access_mode) as file:
print(file.readline())
file.close()
Result:
3. You can use readlines()
Observe that the result where any text in the new line contains \n and uses single quotes. All the new line texts are elements of a list.
Code:
with open(filename, access_mode) as file:
print(file.readline())
Result:
You must use the close() function to close any file opened to read or write. If you perform this, it will clear all the buffers and close the file.
The syntax is as follows:
file.close()
The following is an example of opening and closing a file:
Code:
file = open("sample.txt", "r")
for lines in file:
print(lines)
file.close
Result:
While working with files, in most instances, you are required to write in a file. The access mode for this is ‘w.’
You can write to a file using the following methods:
You create a new file using this access mode with open() if a file already does not exist.
Note: Ensure that you give a correct path with the accurate filename; otherwise, it will throw an error.
Let’s create a notepad file and write some text into it. You will observe that a new file is created (if the file does not exist). You can run the code provided to write in this file.
Code:
newFile = open("newFile.txt", "w")
Let’s add the first line into the .txt file that you created. Once you run the program, you will observe that the new line is visible in newFile.txt.
newFile = open("newFile.txt", "w")
# Adding the first line
newFile.write("This is the first line")
newFile.close
Result:
This mode appends new lines in a text file. For example, let’s suppose you want to add multiple lines of content as shown:
India
America
Australia
United Kingdom
Antarctica
You can add this to the newFile.txt using the following:
Code:
newFile = open("newFile.txt", "a+")
# List of countries
Country = ["India\n", "America\n", "Australia\n", "United Kingdom\n", "Antarctica\n"]
# Appending the list to the file
newFile.writelines(Country)
newFile.close()
Result:
JSON contains an array of key: value pairs. Each pair is separated using a comma, and the curly brackets {} enclose the items. To work with JSON-type files, you must import the JSON library.
When you read the file with read(), you are reading strings from a file. Thus, even if you enter an integer value, it is converted to a string. To use these string values as integer values, you must convert them to integers using the int() function.
To read a JSON file, you use the load function along with open().
Code:
import json
# Loading and opening the file
# The file is already existing in the folder
file = json.load(open("sample.json"))
print(file)
Result:
The sample.json file contains the following content. You’ll find the same printed when you execute the provided code.
To write into a JSON file, you use the ‘w’ access mode: similar to the previous examples. However, you are required to dump values into the file.
Code:
import json
# Creating content to dump in the file
country = {"country1": "India",
"country2": "America",
"country3": "United Kingdom"}
# Opening a new file to write in JSON
with open("newFile.json", "w") as file:
json.dump(country, file)
Result:
A new file named newFile.json is created that contains the country dictionary in it.
Context manager allows you to allocate and release resources only when you require them. The most popular example of a content manager is the with statement.
The main advantage of using this statement is that it ensures your file is closed without providing nested blocks. One of the common uses of this manager is locking and unlocking resources and closing any opened files while working with them. Here’s an example shown with the following code:
with open('sample.txt', "w") as opened_file:
opened_file.write("Hello!")
The equivalent code of this is shown in the following code:
file = open('sample.txt', "w")
try:
file.write("Hello!")
finally:
file.close()
If you compare these two codes, you’ll observe that most of the excess boilerplate lines are eliminated using the with statement.
Sometimes you have to work with multiple files simultaneously. You have to write or read from these files. You can use the following code to perform this easily:
book_path = 'book_list.txt'
book_rev_path = 'book_list_reversed.txt'
with open(book_path, 'r') as reader, open(book_rev_path, 'w') as publisher:
book = reader.readlines()
publisher.writelines(reversed(book))
Question 1: What is read() in Python?
The read() function in Python is used to read at most n bytes from a file associated with a provided file.
Question 2: How do I write in a file in Python?
In Python, there are two ways that you can use to write in a file. They are: write() and writelines().
Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
————
Article contributed by Problem Setters Official
Attend our webinar on
"How to nail your next tech interview" and learn