Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.unlink() Method

Python3 os.unlink() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.unlink() method deletes a file; if the file is a directory it returns an error.

Syntax

The **unlink()** method syntax is as follows:


os.unlink(path)

Parameters

Return Value

This method has no return value.

Example

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


#!/usr/bin/python3

import os, sys

# List directory
print ("目录为: %s" %os.listdir(os.getcwd()))

os.unlink("aa.txt")

# Deleted directory
print ("删除后的目录为 : %s" %os.listdir(os.getcwd()))

The output of the above program is:


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

Python3 OS File/Directory Methods Python3 OS File/Directory Methods