How to print the contents of a file in Python

In this article, we will learn how to print the contents of a file in python. We will be discussing multiple approaches to print the contents of a file. The file name and its contents which we will be using in the code is as follows,

File name: file.txt

Contents of the file

Hello world!
python is very
easy to learn

Table Of Contents

Printing the contents of a file using read()

In Python, a file object has a built-in read() method, and it takes the number of bytes to be read from the file as input and returns the specified number of bytes from the file. If no arguments are passed to the read() method then the entire contents of the file will be returned.

Approach :

  1. Create an object of the file to be read using the open() method.
  2. Call the read() method on the file object. It returns a string initialized with the contents of the file.
  3. Print the string that contains the file contents.

Source Code

# opening the file
fileObj=open("file.txt")

# reading contents of the file
fileContent=fileObj.read()

# printing the contents of the file
print(fileContent)

# closing the file
fileObj.close()

Output:

Hello world!
python is very
easy to learn

Details about APIs used,

Syntax to get a file object :

The open() method opens a file and returns it as a file object.

open(file_path, mode)
  • Parameters:
    • file_path : The path of the file.
    • mode : The mode in which the file needs to be opened. read (r), write (w), append (a). By default read mode.
  • Returns:
    • Returns a file object.

Syntax of read() method of file object

read(size)
  • Parameters:
    • size : number of bytes to be read from the file, By default -1.
  • Returns:
    • Contents of the file.

Printing only the first n bytes of a file using read()

To read the first n of bytes of the file, we need to pass the n value to the read() method, which will return a string with the first n bytes of the file.

Approach:

  1. Create an object of the file to be read using the open() method.
  2. Call the read() method on the file object by passing n, for example, n=5. It returns a string initialized with the first 5 bytes of the file.
  3. Print the string.

Source Code

# opening the file
fileObj=open("file.txt")

# reading contents of the file
fileContent=fileObj.read(5)

# printing the contents of the file
print(fileContent)

# closing the file
fileObj.close()

Output:

Hello

Printing the contents of a file using readline()

Python has a built-in readline() method, and it takes the number of bytes to be read from the line as input and returns the specified number of bytes from the line. If no arguments are passed to the readline() method, then one line of the file will be returned. If file pointer has reached the end of file, then it returns None.

Syntax of readline() method

readline(size)
  • Parameters:
    • size : number of bytes to be read from the line, By default returns a single line.
  • Returns:
    • Returns Contents of a single file.

Approach:

  1. Create an object of the file to be read using the open() method.
  2. Call the readline() method on the file object in a loop till eof is reached.
  3. During loop, print each line/string returned by the readline() function.

Source Code

# opening the file
fileObj=open("file.txt")
fileContent = "None"

while fileContent:
    # reading contents of the file
    fileContent = fileObj.readline()
    fileContent = fileContent.strip()
    # printing the contents of the file
    print(fileContent)

# closing the file
fileObj.close()

Output:

Hello world!
python is very
easy to learn

Printing the contents of a file using readlines()

In Python, file object has a built-in readlines() method, and it returns a list of strings. Where, each string in the list represents a line in the file.

Syntax of readlines() method

readlines(hint_size)
  • Parameters:
    • hint_size : Optional, If the number of bytes returned exceeds the hint_size then the remaining lines in the file will not be returned.
  • Returns:
    • Returns file content as a list of strings.

Approach:

  1. Create an object of the file to be read using the open() method.
  2. Call the readlines() method on the file object. It returns a list of strings, and each string in the list represents a line in the file.
  3. Print the list of strings.

Source Code

# opening the file
fileObj=open("file.txt")

# reading contents of the file
fileContentList=fileObj.readlines()

# printing the contents of the file
for line in fileContentList:
    print(line.strip())

# closing the file
fileObj.close()

Output:

Hello world!
python is very
easy to learn

Summary

Great! you made it, We have discussed all possible methods to print the contents of a file in Python. Happy learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top