The examples in this article describe how Python creates and deletes directories. Share to everyone for your reference. The specific analysis is as follows:
The following code creates a directory and then invokes the custom Deletedir function to delete the entire directory
#--------------------------------------# name:create_directory.py# Author:kevin harris# last modified:02/13/04 # description:this Python Script demonstrates# How to create a single# new directory as well as delete a directory# and everything # it contains. The script would fail # if Encountewrs a read-only# file#--------------------------------------import os#-- ------------------------------------# Name:deletedir () # Desc:deletes a directory and its content recursively.#-------- ------------------------------def deletedir (dir): for name in Os.listdir (dir): File = dir + "/" + name if not O S.path.isfile (file) and Os.path.isdir (file): Deletedir (file) # It's another directory-recurse in to it ... Else:os.remove (file) # It's a file-remove it ... os.rmdir (dir) #--------------------------------------# Script Entry point...#--------------------------------------# Creating A new directory is easy ...Os.mkdir ("Test_dir") # Pause for a moment so we can actually see the directory get Created.input (' A directory called ' Te S_dir "was created.\n\npress Enter to delete it. ') # Deleting It can be a little harder since it could contain files, so we'll need # to write a function to help us out HERE.D Eletedir ("Test_dir");
Hopefully this article will help you with Python programming.