Whats the difference between file.read() and reader(file)?

Screen Link:

I am confused with these different notations to read the file. Can Someone explain why we are using different syntactical notations to read the file?

import csv,chardet
with open('kyoto_restaurants.csv',mode ='rb') as file:
    raw_bytes = file.read(3)
    encoding_name = chardet.detect(raw_bytes)['encoding']
with open('kyoto_restaurants.csv', encoding = encoding_name) as file:
    rows = list(csv.reader(file))

They’re doing completely different things.

The first one reads only the first three bytes of the file and tries to guess how it was encoded based on these three bytes.

The second one uses the result of the first function to read the file.

1 Like