Question: How do I delete a file or folder?

Answer: 
Deleting a file or folder in Python
===================================

**There are multiple ways to Delete a File in Python but the best ways are the following:**

1. **os.remove()** removes a file.
2. **os.unlink()** removes a file. it is a Unix name of remove() method.
3. **shutil.rmtree()** deletes a directory and all its contents.
4. **pathlib.Path.unlink()** deletes a single file The pathlib module is available in Python 3.4 and above.

os.remove()
===========

Example 1: Basic Example to Remove a File Using os.remove() Method.
-------------------------------------------------------------------

```
import os
os.remove("test_file.txt")
print("File removed successfully")

```

Example 2: Checking if File Exists using os.path.isfile and Deleting it With os.remove
--------------------------------------------------------------------------------------

```
import os
#checking if file exist or not
if(os.path.isfile("test.txt")):
    #os.remove() function to remove the file
    os.remove("test.txt")
    #Printing the confirmation message of deletion
    print("File Deleted successfully")
else:
print("File does not exist")
#Showing the message instead of throwig an error

```

Example 3: Python Program to Delete all files with a specific extension
-----------------------------------------------------------------------

```
import os 
from os import listdir
my_path = 'C:\Python Pool\Test\'
for file_name in listdir(my_path):
    if file_name.endswith('.txt'):
        os.remove(my_path + file_name)

```

Example 4: Python Program to Delete All Files Inside a Folder
-------------------------------------------------------------

To delete all files inside a particular directory, you simply have to use the \* symbol as the pattern string.
#Importing os and glob modules
import os, glob
#Loop Through the folder projects all files and deleting them one by one
for file in glob.glob("pythonpool/\*"):
os.remove(file)
print("Deleted " + str(file))

os.unlink()
===========

os.unlink() is an alias or another name of os.remove() . As in the Unix OS remove is also known as unlink.
Note: All the functionalities and syntax is the same of os.unlink() and os.remove(). Both of them are used to delete the Python file path.
Both are methods in the os module in Python’s standard libraries which performs the deletion function.

shutil.rmtree()
===============

Example 1: Python Program to Delete a File Using shutil.rmtree()
----------------------------------------------------------------

```
import shutil 
import os 
# location 
location = "E:/Projects/PythonPool/"
# directory 
dir = "Test"
# path 
path = os.path.join(location, dir) 
# removing directory 
shutil.rmtree(path) 

```

Example 2: Python Program to Delete a File Using shutil.rmtree()
----------------------------------------------------------------

```
import shutil 
import os 
location = "E:/Projects/PythonPool/"
dir = "Test"    
path = os.path.join(location, dir) 
shutil.rmtree(path) 

```

pathlib.Path.rmdir() to remove Empty Directory
==============================================

Pathlib module provides different ways to interact with your files. Rmdir is one of the path functions which allows you to delete an empty folder. Firstly, you need to select the Path() for the directory, and then calling rmdir() method will check the folder size. If it’s empty, it’ll delete it.

This is a good way to deleting empty folders without any fear of losing actual data.

```
from pathlib import Path
q = Path('foldername')
q.rmdir()

```
```
import os

folder = '/Path/to/yourDir/'
fileList = os.listdir(folder)

for f in fileList:
    filePath = folder + '/'+f

    if os.path.isfile(filePath):
        os.remove(filePath)

    elif os.path.isdir(filePath):
        newFileList = os.listdir(filePath)
        for f1 in newFileList:
            insideFilePath = filePath + '/' + f1

            if os.path.isfile(insideFilePath):
                os.remove(insideFilePath)

```