Python Programming 101 - File handling

Python Programming 101 - File handling

Python Programming 101 - File handling
Python Programming 101 - File handling

Join our telegram channel and group :)

Telegram Group: https://t.me/randomskoolQnA
Telegram Channel: https://t.me/randomskool


In this tutorial, we will learn how to deal with file operations using python programming. The files are crucial for the Operating System ( in files only OS stores all the necessary configuration setup ) as well as for Users ( in files the user store their data permanently and want their data to handled carefully ).

Be cautious while dealing with files.
Yeah, True Statement.
One single wrong action can destroy/corrupt the file. 

Few operations we need to use properly at the time of dealing files.

1. Make a habit of opening a file in read mode.
2. If writing is required in a file, check whether the file exists or not. If not, then good to open the file in write mode. If the file exists and there is some content also present, make sure to open the file in append mode. Otherwise, the whole file is overwritten. 
3. Once work completed, make sure to close the file gracefully and properly.

Steps of file handling using python programming.

1. Opening a file
2. Reading/Writing a file
3. Closing a file


File I/O Basics

"r" - open file for reading - default operations
"w" - open file for writing
"x" - creates file if not exists
"a" - append a file
"t" - text mode - default operation
"b" - binary mode
"+" - read and write

File Operation

1. read() - Return the specified no. bytes from the file. Default is -1 which means the whole file.
2. readline() - Return one line at a time.
3. readlines() - Returns all lines as a list.
4. tell() - Tell the location of the file pointer
5. seek() - Reset the file pointer ( by specifying 0 ) or we can update the position using the location or numbers of characters ( by specifying any other number than 0 ).
6. write() - Write the content to file.
7. close() - Close the file.

Example of Reading and Writing a file




For Code download. Please visit here.


Other ways of Closing a file

There are two ways of closing a file using try ... finally, block and using context managers.

1. Using try ... and finally 
   
Syntax:
   
     try:
         file operation statements
         .
         .
         .
     finally:
        file close statements.

Note: file closing statements always put in finally block because finally, the block will always run after the completion of the try block.

2. Using Context Manager

In the context manager concept, there is no need to explicitly define the closing statement. File closing can be done with the help of with statement.

Syntax:

     with open("filename.txt","r") as file:
             file operation statements
             ...
             ...
             ...


The upcoming tutorial will be the last in Python Programming 101 series !!!
Hasta La Vista Amigos !!! C'est la vie !!! Yehi Zindagi hai ...

Click here for the next tutorial.

For Complete Python Programming 101 tutorial series ( Click here ). 

For Queries and Contact there are multiple ways:

1. From telegram search @randomskoolQnA and @randomskool 
2. From Contact Us Page
3. From Comment 

For Daily Updates Please subscribe to the feed. 

For Job-related Updates Please fill the subscriber feedback form.

Join our telegram channel and group :)

Telegram Group: https://t.me/randomskoolQnA
Telegram Channel: https://t.me/randomskool


Till then happy coding !!!


0 Response to "Python Programming 101 - File handling"

Post a Comment

Hey Random,

Please let me know if you have any query :)

Adsense

Adsense

Adsense

Adsense