Easy Tutorial
❮ Ref Math Nan Python Mongodb Delete Document ❯

Python3 os.unlink() Method

Python3 OS File/Directory Methods


Overview

The os.unlink() method is used to delete a file. If the file is a directory, an error is returned.

Syntax

The syntax for the unlink() method is as follows:

os.unlink(path)

Parameters

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the unlink() method:

#!/usr/bin/python3

import os, sys

# List directory
print ("Directory is: %s" % os.listdir(os.getcwd()))

os.unlink("aa.txt")

# Directory after deletion
print ("Directory after deletion: %s" % os.listdir(os.getcwd()))

Output of the above program would be:

Directory is:
['a1.txt', 'aa.txt', 'resume.doc']
Directory after deletion:
['a1.txt', 'resume.doc']

Python3 OS File/Directory Methods

❮ Ref Math Nan Python Mongodb Delete Document ❯