---
title: "How to delete files in Python?"  
description: "How to delete files in Python?"  
author: "ICSM Computer"  
published: 2025-11-02  
updated: 2025-11-02  
canonical: https://www.mindstick.com/interview/34400/how-to-delete-files-in-python  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 3 minutes  

---

# How to delete files in Python?

To [**delete a file in Python**](https://www.mindstick.com/interview/34398/explain-the-python-file-open), you can use the built-in `os` or `pathlib` modules.

Here are the most common methods:

### Method 1: Using `os.remove()`

```python
import os

file_path = "example.txt"

# Check if file exists before deleting
if os.path.exists(file_path):
    os.remove(file_path)
    print(f"{file_path} deleted successfully.")
else:
    print("File does not exist.")
```

**When to use:**\
Use this for simple file deletion where you just need to remove a file by path.

### Method 2: Using `os.unlink()`

```python
import os

file_path = "example.txt"

try:
    os.unlink(file_path)
    print("File deleted successfully.")
except FileNotFoundError:
    print("File not found.")
```

> `os.remove()` and `os.unlink()` are essentially the same — `unlink()` is the underlying system call.

### Method 3: Using `pathlib.Path.unlink()` (Modern Approach)

```python
from pathlib import Path

file = Path("example.txt")

if file.exists():
    file.unlink()
    print(f"{file} deleted successfully.")
else:
    print("File does not exist.")
```

**When to use:**\
Prefer `pathlib` if you are working with modern Python (3.6+) and want object-oriented path handling.

### Method 4: Deleting Multiple Files with a Pattern

```python
import glob
import os

for file_path in glob.glob("*.log"):  # delete all .log files in current directory
    os.remove(file_path)
    print(f"Deleted: {file_path}")
```

### Error Handling Tip

Always wrap file deletion in a try-except block to avoid crashes:

```python
import os

try:
    os.remove("example.txt")
except FileNotFoundError:
    print("File not found.")
except PermissionError:
    print("Permission denied.")
except Exception as e:
    print(f"Error: {e}")
```

## Answers

### Answer by ICSM Computer

To [**delete a file in Python**](https://www.mindstick.com/interview/34398/explain-the-python-file-open), you can use the built-in `os` or `pathlib` modules.

Here are the most common methods:

### Method 1: Using `os.remove()`

```python
import os

file_path = "example.txt"

# Check if file exists before deleting
if os.path.exists(file_path):
    os.remove(file_path)
    print(f"{file_path} deleted successfully.")
else:
    print("File does not exist.")
```

**When to use:**\
Use this for simple file deletion where you just need to remove a file by path.

### Method 2: Using `os.unlink()`

```python
import os

file_path = "example.txt"

try:
    os.unlink(file_path)
    print("File deleted successfully.")
except FileNotFoundError:
    print("File not found.")
```

> `os.remove()` and `os.unlink()` are essentially the same — `unlink()` is the underlying system call.

### Method 3: Using `pathlib.Path.unlink()` (Modern Approach)

```python
from pathlib import Path

file = Path("example.txt")

if file.exists():
    file.unlink()
    print(f"{file} deleted successfully.")
else:
    print("File does not exist.")
```

**When to use:**\
Prefer `pathlib` if you are working with modern Python (3.6+) and want object-oriented path handling.

### Method 4: Deleting Multiple Files with a Pattern

```python
import glob
import os

for file_path in glob.glob("*.log"):  # delete all .log files in current directory
    os.remove(file_path)
    print(f"Deleted: {file_path}")
```

### Error Handling Tip

Always wrap file deletion in a try-except block to avoid crashes:

```python
import os

try:
    os.remove("example.txt")
except FileNotFoundError:
    print("File not found.")
except PermissionError:
    print("Permission denied.")
except Exception as e:
    print(f"Error: {e}")
```


---

Original Source: https://www.mindstick.com/interview/34400/how-to-delete-files-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
