Blog Detail

Covid-19 Global Tracker

preview image Programming
by jitender yadav, Feb 2, 2018, 7:29:35 PM | 2 minutes |

Handling files and folders using Python

Python is a great language as using it we can create a web application, IoT application, or any automation script, etc. Here in this blog, we are going to learn a very basic file and folder operation where we will be deleting the files and folders. For these operations, we are going to use the os library of Python.

Deleting a single file:

First I am going to cover how to remove a single file from a directory/folder using python.

Below is the code and explanation for removing a single file in python:

               import os
               folder = '/path/to/folder'        
               file_path = os.path.join(folder, 'file_name.ext')                  
               try:                     
                    if os.path.isfile(file_path):
                         os.unlink(file_path)
               except Exception as e:
                    print(e)                         

 

For deleting All files in a folder: 

We can also delete all files from a directory using Pyhon and for that we can write the following code:

               import os
               folder = '/path/to/folder'        
               for the_file in os.listdir(folder):
                     file_path = os.path.join(folder, the_file)                   
                     try:                     
                         if os.path.isfile(file_path):
                             os.unlink(file_path)
                     except Exception as e:
                          print(e)   

 

Deleting a folder will all files:     

If we want to delete a folder with complete files and folders then we can write the following code:    

               import os
               folder = '/path/to/folder'        
               for the_file in os.listdir(folder):
                     file_path = os.path.join(folder, the_file)                   
                     try:                     
                         if os.path.isfile(file_path):
                             os.unlink(file_path)
                         elif os.path.isdir(file_path): 
                               shutil.rmtree(file_path)
                     except Exception as e:
                          print(e)

 

I hope this is helpful and in case of any queries please let me know.


Comments (3)

  • user image
    Anurag Srivastava
    Feb 3, 2018, 5:07:09 AM

    It is a nice article for a person who want to start using python for sytem programming.

  • user image
    jitender yadav
    Feb 3, 2018, 1:09:26 PM

    Thanks! Anurag.

  • user image
    jitender yadav
    Feb 3, 2018, 1:12:16 PM

    you can also simply append file name with folder name with + operator

Leave a comment

Related Blogs

Elastic Stack Interview Questions

Sep 12, 2020, 3:58:55 PM | Anurag Srivastava

Introduction to Kibana

Aug 1, 2020, 6:19:45 PM | Anurag Srivastava

Create a Chess board in PHP

Mar 9, 2020, 8:45:41 AM | Rocky Paul

Top Blogs

Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

Elasticsearch Rest API

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava

Metrics Aggregation in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava

Introduction to Elasticsearch Aggregations

Aug 14, 2018, 4:47:56 PM | Anurag Srivastava

Basics of Data Search in Elasticsearch

Aug 4, 2018, 7:02:21 AM | Anurag Srivastava