Question: How do I delete a file or folder?

Answer: 
* [`os.remove()`](http://docs.python.org/library/os.html#os.remove) removes a file.
* [`os.rmdir()`](http://docs.python.org/library/os.html#os.rmdir) removes an empty directory.
* [`shutil.rmtree()`](http://docs.python.org/library/shutil.html#shutil.rmtree) deletes a directory and all its contents.

---

[`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) objects from the Python 3.4+ [`pathlib`](https://docs.python.org/3/library/pathlib.html) module also expose these instance methods:

* [`pathlib.Path.unlink()`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink) removes a file or symbolic link.
* [`pathlib.Path.rmdir()`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.rmdir) removes an empty directory.
Python syntax to delete a file
------------------------------

```
import os
os.remove("/tmp/<file_name>.txt")

```

Or

```
import os
os.unlink("/tmp/<file_name>.txt")

```

Or

**[pathlib](https://docs.python.org/3/library/pathlib.html#module-pathlib) Library for Python version >= 3.4**

```
file_to_rem = pathlib.Path("/tmp/<file_name>.txt")
file_to_rem.unlink()

```

Path.unlink(missing\_ok=False)
------------------------------

Unlink method used to remove the file or the symbolik link.

> 
> If missing\_ok is false (the default), FileNotFoundError is raised if the path does not exist.  
> 
> If missing\_ok is true, FileNotFoundError exceptions will be ignored (same behavior as the POSIX rm -f command).  
> 
> Changed in version 3.8: The missing\_ok parameter was added.
> 
> 
> 

Best practice
-------------

1. First, check whether the file or folder exists or not then only delete that file. This can be achieved in two ways :  

a. `os.path.isfile("/path/to/file")`  

b. Use `exception handling.`

**EXAMPLE** for `os.path.isfile`

```
#!/usr/bin/python
import os
myfile="/tmp/foo.txt"

## If file exists, delete it ##
if os.path.isfile(myfile):
    os.remove(myfile)
else:    ## Show an error ##
    print("Error: %s file not found" % myfile)

```

### Exception Handling

```
#!/usr/bin/python
import os

## Get input ##
myfile= raw_input("Enter file name to delete: ")

## Try to delete the file ##
try:
    os.remove(myfile)
except OSError as e:  ## if failed, report it back to the user ##
    print ("Error: %s - %s." % (e.filename, e.strerror))

```

### RESPECTIVE OUTPUT

```

Enter file name to delete : demo.txt
Error: demo.txt - No such file or directory.

Enter file name to delete : rrr.txt
Error: rrr.txt - Operation not permitted.

Enter file name to delete : foo.txt

```

### Python syntax to delete a folder

```
shutil.rmtree()

```

Example for `shutil.rmtree()`

```
#!/usr/bin/python
import os
import sys
import shutil

# Get directory name
mydir= raw_input("Enter directory name: ")

## Try to remove tree; if failed show an error using try...except on screen
try:
    shutil.rmtree(mydir)
except OSError as e:
    print ("Error: %s - %s." % (e.filename, e.strerror))

```