Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - WRITE TO FILE


So in the previous tutorial, we have created file 'myfirstfile.txt' using the 'open( )' Function.


myfile = open("myfirstfile.txt", "xt")

Now, let us see, how can we write something to the file.


There are two modes of writing to a File :


  1. "w" - Write Mode - Write mode is used to write something to the file. It overwrites the contents of the file. And if the file is not present, it creates a new file.
  2. "a" - Append Mode - Append mode is used to write something at the end of the file. And if the file is not present, it creates a new file.

So, we have created the file, 'myfirstfile.txt'. Now, let us write the below paragraph to the file.


In a huge pond,
there lived many fish.
They were arrogant and
never listened to anyone.

Example :



myfile = open("myfirstfile.txt", "w")

para = """In a huge pond, 
there lived many fish.
They were arrogant and 
never listened to anyone.""" 
        
myfile.write(para)
myfile.close()


Output :



Now, if you open the File, 'myfirstfile.txt', you can find the below contents,


java_Collections

  1. So, what we have done in the above example is, opened the file in Write Mode(i.e. 'w').

    myfile = open("myfirstfile.txt", "w")


    We haven't specified the 'Text Mode' (i.e. 't') because it is the default mode.
  2. Then we have initialise the paragraph in a variable, 'para' using triple quotes.

    para = """In a huge pond,
    there lived many fish.
    They were arrogant and
    never listened to anyone."""
  3. After that we have used the 'write( )' Function to write the paragraph to the file.

    myfile.write(para)
  4. Finally, we close the file using the 'close( )' Function.

    myfile.close( )

Now, let us say, we want to add three more lines to the above paragraph.


In this pond,
there also lived
a kind-hearted crocodile.

Example :



myfile = open("myfirstfile.txt", "a")

para = """\nIn this pond, 
there also lived 
a kind-hearted crocodile.""" 
        
myfile.write(para)
myfile.close()


Output :



Now, if you open the File, 'myfirstfile.txt', you can find the below contents,


java_Collections

  1. So, what we have done in the above example is, opened the file in Append Mode(i.e. 'a').

    myfile = open("myfirstfile.txt", "a")


    We haven't specified the 'Text Mode' (i.e. 't') because it is the default mode.
  2. Then we have initialised the paragraph in a variable, 'para' using triple quotes. So that we could append those three more lines to the existing File.

    para = """\nIn this pond,
    there also lived
    a kind-hearted crocodile."""


    We have put the escape character '\n'. So that the new lines are added to the next line of the existing contents of the file.
  3. After that we have used the 'write( )' Function to write the paragraph to the file.

    myfile.write(para)
  4. Finally, we close the file using the 'close( )' Function.

    myfile.close( )

Next, we will see, how can we read the above file in Python.