Python Programming Langauge




As shown in the following example

  • File named 'Using-Module.py' is opened in the read mode using open() function specifying read mode using 'r' character and 'fp' is the file pointer. Entire content of the file 'Using-Module.py' is read into the variable 'content' using the read() function and the contents of the variable 'content' is printed. File is closed after the operation.
  • Again the file 'Using-Module.py' is opened in the read mode. Now each line of the file is read using readline() function and the read line is stored in the variable 'aLine' and that line is printed one after another.


As shown in the following example

  • File named 'Using-Module.py' is opened in the read mode using open() function and 'fp' is the file pointer. Entire content of the file 'Using-Module.py' is read line by line into the variable 'lines' using the readlines() function and the content of the variable 'lines' is printed line by line inside the for loop and file is closed.
  • Again the file 'Using-Module.py' is opened in the read mode. Now specified number of bytes from the beginning of the file is read using read() function by specifying the number of bytes to be read within the paranthesis and in this case it 13 bytes. The 13 bytes are stored in the variable 'bytes' and it is printed.


As shown in the following example

  • File named 'MGM.txt' is opened in the write mode using open() function specifying write mode using 'w' character and 'fp' is the file pointer. Now if the file named 'MGM.txt' is present it will be overwritten or a new file will be created. Using write() function content is written into the file. In this the variable 'content' has string that will be stored in the file and file is closed.
  • Again the file 'MGM.txt' is opened in the append mode using 'a' character during the opening of the file. The the required content is appended at the end of the file using the write() function. Again the file 'MGM.txt' is opened in the read mode and the contents are displayed and at the end it is closed.


Modules<< Previous
Next >> Date-Time

Python OOP concepts, Using JSON & XML, Sending Mail, Using HTTP will be continued....

By: Vishwanath Pai M

Reference: IOT by Arshdeep Bahga, Vijay Madisetti

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus