Python Program to Read a file

In the following example of a Python program that demonstrates how to read a file :
Program :
try:
    with open('example.txt', 'r') as file:
        contents = file.read()
        print(contents)
except FileNotFoundError as e:
    print(f"File not found: {e}")
except Exception as e:
    print(f"An error occurred: {e}")
In this example, the `open()` function is used to open the file named "example.txt" in read-only mode, and the `with` statement is used to ensure that the file is properly closed after it has been read.

The contents of the file are then read into the `contents` variable using the `read()` method. Finally, the contents are printed to the console.

Note that the `try` block is used to catch any exceptions that might occur while reading the file, such as a `FileNotFoundError` if the file does not exist, or an `IOError` if there is a problem with reading the file.

If an exception occurs, the code in the `except` block is executed instead, which prints an error message.

You can replace the contents of "example.txt" with the file path of the file you want to read. Make sure that the file is in the same directory as your Python script, or provide the full path to the file if it is located elsewhere.