Home>

I'm writing code to read an html file.
I'm a beginner in Python.
Environment is Windows 10
Python3
Install wkhtmltopdf to create PDF from HTML
Install the pdfkit module to use a module called "pdfkit" to run wkhtmltopdf from Python.

The following error message occurred while implementing the function to read the html file.

### Error message

FileNotFoundError Traceback (most recent call last)
in
---->1 with open (template_html,'rt', encoding ='utf-8') as fp:
2 text = fp.read ()

FileNotFoundError: [Errno 2] No such file or directory:'./User/template.html'

Corresponding source code
#Specify the file path
template_html ='./User/template.html'
temp_file ='./Users/User/__tmp.html'
output_file ='./output.pdf'
with open (template_html,'rt', encoding ='utf-8') as fp:
    text = fp.read ()
What I tried

I read various sites and checked the syntax.
Because it says No such file or directory

with open (./Users/User/template.html,'rt', encoding ='utf-8') as fp:
I tried to rewrite it to.
SyntaxError: invalid syntax, so modify mode ='rt'
Still, the result was the same.

I just don't know what's wrong with the syntax.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    I'm getting a SyntaxError because the file path isn't enclosed in "'" or "" "and isn't a string.

    And the file path written there is "./Users/User/template.html", but the one written first is "./User/template.html", so due to a mistake in this path Wasn't it said FileNotFoundError?

  • Answer # 2

    FileNotFoundError: [Errno 2] No such file or directory:'./User/template.html'

    The error is that the specified file does not exist.
    The file is specified incorrectly, so let's review it.
    For beginners, full pass (/There are few mistakes to specify the path starting with).

    with open (./Users/User/template.html,'rt', encoding ='utf-8') as fp:
    SyntaxError: invalid syntax

    Since the string is not quoted, it is not considered a string and is a syntax error. In other words, it got worse.