-2

I wanted to read an HTML file with open("page.html","r"). I was able to do it without problem. My question is what files does Python consider to be text files and what binary files? For example, are .css files text files too? And what happens if I want to read a binary file without binary mode?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Edu Grando
  • 435
  • 1
  • 4
  • 5

2 Answers2

1

The mode in which you read a file really depends on what you want to do with it. You can open any file as either. However, how the data is read / written is very different. Reading a binary file not in binary mode would be very similar to just opening that same binary in a text editor; the data would be interpreted as ascii data instead of binary data and weird things would happen.

stackunderflow
  • 202
  • 1
  • 7
0

Every file can be seen as either binary 0 and 1 s or ASCII characters (which is text). When open a file in notepad or an programming editor like sublime, you see that file as ASCII characters or text. Files like .html .css files are meaningful when you open and read them as ASCII characters text format but still you can open and read them in binary mode which would be the binary value of each character and not human readable.

About some files like .exe you still are able to open and read them as ASCII characters but they are not generated in ASCII characters so when you open them as text in notepad you see non-human readable characters, it is just viewing lets say every 8 byte as an ASCII character but in fact those bytes may be meaningful separately for example when they are read by computer cpu.

bardia zamanian
  • 49
  • 1
  • 1
  • 5