Writing to File

- It creates a new file and add new data to it
- If you choose a file that already exist, it will empty the file & overwrite it

Overview

- First Way

    - Opening the File in Write Mode

    - Adding new data to the file

    - Closing the File

    - Opening the File in Read Mode to check if new data is added

    - Closing the File

- Second way (open with)

    - Opening the File in write mode & Adding new data to it

    - Reading the File

First Way

Opening the File in Write Mode

In [11]:
students = open("student2.csv","w")

Adding a new data to the file

- using \n for adding to a new line 
In [12]:
students.write("\n1, will, 29 , male")
Out[12]:
19

Closing the File

In [13]:
students.close()

Reading the File

- Checking if new data is added
In [14]:
students = open("student2.csv","r")

print(students.read())
1, will, 29 , male

Closing the File

In [15]:
students.close()

Second Way

Adding new data to the File

- using \n for adding to a new line 
In [9]:
with open("student4.csv", "w") as students:
    
    student = students.write("\n6, rose, 29 , female")
    
    print(student)
21

Reading the file

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

6, rose, 29 , female