Reading File

Overview

- First Way
    - Opening the File in Read Mode
    - Checking if the File is Readable
    - Reading the File 

        - using read()
        - using Loop & readlines()

    - Closing the File

- Second Way (open with)

Opening the File in Read Mode

In [6]:
students = open("students.csv","r")

Checking if the File is Readable

In [7]:
print(students.readable())
True

Reading the File

- using read()
- using Loop

Reading the File using read()

In [3]:
print(students.read())
1, Sarah, 29 , female
2, John, 30, male
3, Jack, 28, male
4, Anna, 27, female

Reading the File ( Loop & readlines() )

In [8]:
for student in students.readlines():

    print(student)
1, Sarah, 29 , female

2, John, 30, male

3, Jack, 28, male

4, Anna, 27, female

Closing the File

In [11]:
students.close()

Second Way

In [10]:
with open("student.csv", "r") as students:
     
    for student in students:
        
        print(student)
1, Sarah, 29 , female

2, John, 30, male

3, Jack, 28, male

4, Anna, 27, female

5, will, 29 , male

6, rose, 29 , female