Search code examples
pythonmhtml

How do I open a MHTML file in Python?


I am trying to open a .mht file in my local system. But it is throwing me File Not Found Error even if the file exists. THis MHTML file is the output of the step recorder in Windows. I want to extract only the text in the file.

The function I used to open the file is:

with open(mi_file) as fp

where mi_file is holding the file name.

The error I am getting is:

Traceback (most recent call last):
  File "C:/Users/swapn/PycharmProjects/StepRecorderAutomation/mhtml_parsing.py", line 40, in <module>
    with open(mi_file) as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'Recording_20200528_1557.mht'

Could someone please help me to find what am I doing wrong here?


Solution

  • Can be solved by two options:

    Option 1: Go to the file location and set that as working directory.

    Option 2: Copy the full path of the file location and save it in mi_file. mi_file = ('path of the file')