0

I'm trying to have Python print out a .HTML file, but every time I run it, it either returns nothing or just b''. I have not received an error while running the code. Here's the code I did:

f = open("D:\Folder\Folder\File.html", "rb")
print(f.read())

I tried using forward slash, back slash, double slash, adding r at the front of the string (the directory for the file), and using r instead of rb. I have also tried using with instead of directly using open.

For clarification, I created the HTML file using Pandas's

df.to_html

Using open has no problems when I am trying to print a .csv file.

Empasteur
  • 9
  • 3
  • 2
    Are you sure there is some text inside it. – codester_09 Aug 12 '22 at 16:17
  • OR the code gives you any error?? – codester_09 Aug 12 '22 at 16:17
  • 2
    If it's a plain text HTML file as the filename suggests, you don't need to open it for reading in binary mode. `open("D:/Folder/Folder/File.html", "r")` will suffice. Also, please use the `with` context manager when opening files. It's much safer, and you don't need to remember to close the file when you're done reading it. – MattDMo Aug 12 '22 at 16:29
  • Welcome to Stack Overflow! This code looks fine to me. Are you sure the HTML file isn't empty? In any case, for debugging help, you need to make a [mre] including example input and expected output, i.e. the contents of the file. If it helps, the backslashes might be messing up the file path; check out [Windows path in Python](/q/2953834/4518341). And to be clear, if Python weren't able to *open* the file, you'd get an error, not empty output. For more tips, check out [How to ask a good question](/help/how-to-ask). – wjandrea Aug 12 '22 at 16:43
  • I am 100% sure there is text inside of it, as the HTML file can load a webpage. There is also no error that appears. However, every time I run the code, it just gives me b''. If it helps, the HTML is generated from pandas dataframe -> html. – Empasteur Aug 12 '22 at 19:18

1 Answers1

0

You have to use a raw string literal, escape the backslashes, or replace the backslashes with forward slashes:

f = open(r"D:\Folder\Folder\File.html", "rb")
print(f.read())

or

f = open("D:\\Folder\\Folder\\File.html", "rb")
print(f.read())

or

f = open("D:/Folder/Folder/File.html", "rb")
print(f.read())
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
  • 1
    as there is no error and also `\F` is the same as `\\F`, this will not solve the op's problem. – codester_09 Aug 12 '22 at 16:20
  • 3
    @codester_09 this is just good practice in general, and I strongly doubt that the OP's filepath is `D:\Folder\Folder\File.html`. There may be `\n`, `\t`, or any number of other actual escape sequences in it. – MattDMo Aug 12 '22 at 16:31
  • @MattDMo That's plausible, but it's more likely the file wouldn't exist in that case and OP would get an error. – wjandrea Aug 12 '22 at 16:44
  • There are a bunch of existing questions that cover this, like [Windows path in Python](/questions/2953834/windows-path-in-python). Please try to avoid duplicating existing info on Stack Overflow. But, it's not clear that this will actually solve the problem OP's experiencing. So if you're posting a *possible* answer, please explain *why* you think this will solve the problem, like what assumptions you're making. Cause if it turns out this is unrelated to the problem, then it's not helpful; it's only going to confuse OP further. For reference, see [answer]. – wjandrea Aug 12 '22 at 16:53