Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Reading and Writing Files in Python

Last updated by Ashwin Ramachandran on Sep 25, 2024 at 11:01 PM | Reading time: 11 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Reading and Writing Files in Python
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

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:

  • File
  • Python File Objects
  • open()
  • Reading From a File
  • Closing Python Files Using close()
  • write()
  • Adding the First Line in the Text File
  • Append Mode
  • Reading in and Writing to a JSON File
  • Creating Your Own Context Manager
  • Working With Two Files Simultaneously
  • Python Interview Questions
  • Frequently Asked Questions

File

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

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:

Graphical user interface, text, applicationDescription automatically generated

Further, there are two types of flat files:

  • Text files, which have an End-of-Line (EOL) character representing each line’s termination. In Python, a new line character (\n) is the default terminator.
  • Binary files, which store 0s and 1s. These types of files return bytes. You’ll use such files extensively when you are working with images, .exe, or .pyc files.

Non-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 File Objects

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. 

open()

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:

  • file represents a path-like object that provides the path to a file that one needs to open.
  • mode represents an optional string that specifies the mode in which a file will open. The different types of strings used in mode along with your functionalities are in the following table:

‘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: 

  • The default mode is ‘r’
  • ‘w+’ and ‘w+b’ open and truncate a file.
  • ‘r+’ and ‘r+b’ open a file without truncating it.
  • buffering is an optional integer value that we use to set the buffering policy.
  • 0 to switch buffering off (it is only allowed in binary mode)
  • 1 to select line buffering (it is only usable in text mode)
  • An integer greater than 1 represents the size in bytes of a fixed-size chunk buffer.
  • encoding represents the encoding used to decode or encode a file. We must use this only in text mode. 
  • errors is an optional string that represents how to handle encoding and decoding errors. We cannot use this in binary mode.
  • newline is something we use to control how universal newlines mode must work. It can be of the following types:
  • None
  • ‘ ‘
  • ‘\n’
    Note: ‘\n’ is the EOF used in a Unix System, and ‘\r\n’ is the EOF used in a Windows system.
  • ‘\r’
  • ‘\r\n’
  • closefd is False, and we use it as a file descriptor rather than a filename provided. 
  • opener represents a customer opener. We can obtain the underlying file descriptor for a file object by calling this argument. It returns a file descriptor. 

Example

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:

Reading From a File

There are three ways you can read from a file. They are as follows:

  • read[x]
  • readline([x])
  • readlines()

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:

TextDescription automatically generated

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:

TextDescription automatically generated with low confidence

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:

Closing Python Files Using close()

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:

  • First, open the sample.txt file using the open() function and store it in a variable file
  • Next, print all the lines in the file using the for loop. 
  • Finally, close the file using the close() function.

Code:

file = open("sample.txt", "r")


for lines in file:

    print(lines)


file.close

Result:

TextDescription automatically generated

write()

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:

  • write(string): This is for text-based files.
  • write(byte_string): This is for binary files.
  • writelines(list)

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")

Adding the First Line in the Text File

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:

Append Mode

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:

Reading in and Writing to a JSON File

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.  

Creating Your Own Context Manager

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. 

Working with Two Files Simultaneously 

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))

Python Interview Questions 

  • What is the difference between lists and tuples?
  • What are the key features of Python?
  • What is __init__ in Python?
  • What is the difference between read-only (r) and read-only (rt) mode?
  • Name the different file-related modules in Python.

 Frequently Asked Questions

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().

Are You Ready to Nail Your Next Coding Interview?

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!

Sign up now!

————

Article contributed by Problem Setters Official


Last updated on: 
October 10, 2024
Author

Ashwin Ramachandran

Head of Engineering @ Interview Kickstart. Enjoys cutting through the noise and finding patterns.

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
First Name Required*
Last Name Required*
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

Reading and Writing Files in Python

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image