Blog Detail

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

How to delete a single file OR all files in folder OR a folder in python

First how to remove a single file in python from a directory/folder 

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: 

               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)   

 

For deleting a folder and its content:     

               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...

Image Credit : Google

About Author

jitender yadav

I am a tech enthusiast and always keen to learn new technologies.

View Profile

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

Metrics Aggregation in Elasticsearch

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

Basics of Data Search in Elasticsearch

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

Elasticsearch Rest API

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

Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

Wildcard and Boolean Search in Elasticsearch

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

Introduction to Elasticsearch Aggregations

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

Create a Pie Chart in Kibana

Dec 24, 2018, 5:25:28 PM | Anurag Srivastava

Create word cloud in Python

Jun 30, 2018, 6:06:45 AM | Anurag Srivastava

Typecasting in PHP for short datatype

Jun 8, 2018, 8:03:52 AM | Lovish Sharma

Top Blogs

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Elasticsearch Rest API

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

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Wildcard and Boolean Search in Elasticsearch

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