/    /  Python – Writing into File

Python – Writing into File:

In this tutorial, we will learn how to write into a file in Python. Writing the data into a file can be done by using the “write()” method. The write() method takes string as an argument. Let us see an example of writing the data into the file called “techsal.csv”.

First creating the data in a file called “techsal.csv”. Below is the data how it looks.

designers, 100, salary, 10000

programmers, 1000, salary, 15000

Dataadmins, 10, salary, 12000

Let us write a line into the same file.

>>> data=open('techsal.csv','r+')

>>>data.write('\nthis is the new line entry\n')

28

Let us read the data from the file.

>>> with open('techsal.csv') as data:

...     out=data.read()

...     print(out)

...

Below is the output:

this is the new line entry

0

programmers, 1000, salary, 15000

Dataadmins, 10, salary, 12000

Finally closing the file:

>>>data.close()