Python 3 - os.remove() Method


Description

The method remove() removes the file path. If the path is a directory, OSError is raised.

Syntax

Following is the syntax for remove() method −

os.remove(path)

Parameters

path − This is the path, which is to be removed.

Return Value

This method does not return any value.

Example

The following example shows the usage of remove() method.

# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")

# listing directories
print ("The dir is: %s" %os.listdir(os.getcwd()))

# removing
os.remove("test.java")

# listing directories after removing path
print ("The dir after removal of path : %s" %os.listdir(os.getcwd()))

Result

When we run the above program, it produces the following result −

The dir is: [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 
   'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 
   'java.ppt', 'ParallelPortViewer', 'test.java'
]
The dir after removal of path : [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 
   'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 
   'java.ppt', 'ParallelPortViewer'
]
python_files_io.htm
Advertisements