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

Screen Link:
https://app.dataquest.io/c/79/m/452/reading-and-writing-to-files/6/encoding-identification-workflow

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']
    print(encoding_name)
    
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