#9 Python Tutorial for Beginners

 Hello Guys,

   In previous tutorial you learned about function , types of function , function with argument , default parameter value and recursion.

In this tutorial you will learn below points:

  • Types of file
  • Opening a file
  • reading file in python
  • readline() function
  • writing files in python
  • mode of opening file
  • with statement

            


Let's get started.

   The RAM  is volatile and all it's contents are lost once program are terminates. In order to persist the data forever , we use files. 
   
    A file is data stored in a storage device. A python program can talk to the file by reading the content from it and writing on it.



Types of file

  • There are two types of file.
             1. Text file (.txt , .c)
             2.  Binary file (.jpg  , .dat)
  


Opening a file

  • Python has an open() for opening files. It takes 2 parameter: file_name and mode.
Syntax :
               open("sample.txt", 'r')

  • sample.txt is file name and 'r' is file mode.


Reading file in python

  • read() function is used to read content from specify a file in python.
Example :

 


readline( ) function

  • readline( ) function is used to read full line at a time.
Example :


Writing files in python

  • write( ) function is used to write into the file.
Example :


Mode of opening file

  • r is open for reading.
  • w is open for writing.
  • a is open for appending.
  • rb will be open for read in binary mode.


With statement

  • with statement is used to open and close the file automatically.

Example :



I hope you like post. If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏


Post a Comment

0 Comments