YouTip LogoYouTip

Os Removedirs

--

Python Basic Tutorial

Python Basic TutorialPython IntroductionPython Environment SetupPython Chinese EncodingPython VS CodePython Basic SyntaxPython Variable TypesPython OperatorsPython Conditional StatementsPython Loop StatementsPython While Loop StatementsPython for Loop StatementsPython Nested LoopsPython break StatementPython continue StatementPython pass StatementPython NumberPython StringPython ListPython TuplePython DictionaryPython Date and TimePython FunctionPython ModulePython File I/OPython File MethodsPython Exception HandlingPython OS File/Directory MethodsPython Built-in Functions

Python Advanced Tutorial

Python Object-OrientedPython Regular ExpressionsPython CGI ProgrammingPython MySQLPython Network ProgrammingPython SMTPPython MultithreadingPython XML ParsingPython GUI Programming (Tkinter)Python2.x vs 3.x Version DifferencesPython IDEPython JSONPython AI DrawingPython 100 ExamplesPython Quiz

Python Exception Handling

Python Built-in Functions

Python2.x Python os.removedirs() Method

Image 3: Python File Methods Python OS File/Directory Methods


Overview

The os.removedirs() method is used to recursively delete directories. Like rmdir(), removedirs() attempts to remove the parent directories only if the child directories are successfully deleted, until an error is thrown (which is basically ignored, as it generally means your folder is not empty).

Syntax

The removedirs() method syntax format is as follows:

os.removedirs(path)

Parameters

  • path -- The directory path to be removed

Return Value

This method has no return value

Example

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

#!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys # List directoryprint "Directory: %s" %os.listdir(os.getcwd())# Remove os.removedirs("/test")# List directory after removalprint "Directory after removal %s :" %os.listdir(os.getcwd())

Executing the above program outputs the following result:

Directory:[ 'a1.txt','resume.doc','a3.py','test' ]Directory after removal:[ 'a1.txt','resume.doc','a3.py' ]

Image 4: Python File Methods Python OS File/Directory Methods

← Os StatOs Readlink β†’