What is the shortest method to open a text file and display its content?

The shortest way to open a text file is by using “with” command in the following manner:

with open(“file-name”, “r”) as fp:
fileData = fp.read()
#to print the contents of the file
print(fileData)

The shortest method to open a text file and display its content in Python would be to use a combination of the with statement and the open() function along with the read() method. Here’s the code:

python
with open('filename.txt', 'r') as file:
print(file.read())

This code opens the file named filename.txt in read mode ('r'), reads its content using the read() method, and then prints it to the console. The with statement is used to automatically close the file after reading its content, ensuring proper resource management.