summary
The OS. Removedirs () method is used to recursively delete a directory. Like rmdir (), if the subfolders are successfully deleted, removedirs () does not try their parent folder until an error is thrown (it is basically ignored because it generally means that your folder is not empty).
Grammar
The syntax format of the removedirs() method is as follows:
os.removedirs(path)
Parameters
path — Directory path to remove
Return value
The method has no return value
example
The following example demonstrates the use of the removedirs() method:
import os,sys
import shutil
dstPath="test/"
print "Before directory deletion: %s" % os.listdir(dstPath)
# Recursively delete directories and files
# shutil.rmtree('test/aa')
# The following two functions are used to delete empty directory files
os.rmdir("test/aa")
#os.removedirs("test/aa")
print "After directory deletion: %s" % os.listdir(dstPath)
Shutil module
shutil.copyfile( src, dst) # Copy from source src to dst. If the current dst already exists it will be overwritten
shutil.move( src, dst) # move the file or rename it
shutil.copymode( src, dst) #just copies its permissions, nothing else is copied
shutil.copystat( src, dst) #copy permissions, last access time, last modified time
shutil.copy( src, dst) #Copy a file to a file or a directory
shutil.copy2( src, dst) # copy the file last access time and modification time on top of the copy also copied over, similar to something like cp -p
shutil.copy2( src, dst) # if the two locations of the file system is the same then it is equivalent to rename operation, just change the name; if it is not in the same file system then it is to do the move operation
shutil.copytree( olddir, newdir, True/Flase) #Copy olddir to newdir, if the 3rd argument is True, then the symbolic link under the folder will be maintained when copying the directory, if the 3rd argument is False, then a physical copy will be generated in the copied directory instead of the symbolic Connections
shutil.rmtree( src ) # recursively delete a directory and all its contents
Read More:
- [Solved] Doris StreamLoad Error: load by MERGE or DELETE need to upgrade table to support batch delete
- [Solved] Opencv Compile Error: (CMake Error: The following variables are used in this project, but they are set to not)
- How to Solve Error: could not list the contents of folder
- Anaconda Error: ‘Destination folder’ contains 2 spaces. This can cause problems with several conda packa
- Harmony OS Error: Failure[INSTALL_FAILED_DEVICE_NOT_SUPPORT_ERROR]
- [Error] expected unqualified-id before ‘delete‘
- [Solved] Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
- [Solved] Mybatis Plus 3.X injection logic delete logicsqlinjector error
- [Solved] os.py“, line 725, in __getitem__ raise KeyError(key) from None KeyError: ‘PATH‘
- [Solved] C++ Compile Error: prerequisites are different [How to Solve]
- Mybatis-Plus logical delete mapping error [How to Solve]
- Solve Maven project running error Failed to clean project: Failed to delete
- Go build package error: TARGET_OS_MAC & IOMasterPort
- [Solved] Oracle Delete the Archive Error: RMAN-08137
- [Solved] Fluent filesysterexception: exists failed, path =’d: \ as’. Plugins’ (OS error: incorrect file name, directory name or volume slogan)
- [Solved] Manifest merger failed: Apps targeting Android 12 and higher are required to specify an explicit
- [Solved] Wepy build watch Error: ERR! Parse WePY config failed. Are you trying to use
- [Solved] graphviz Error: failed to execute [‘dot’, ‘-Tsvg’], make sure the Graphviz executables are on your systems’ PATH
- Angular select Tag Error trying to diff ‘2’. Only arrays and iterables are allowed
- [Solved] webrtc Compile Error: ERROR: The installation of the Chrome OS default fonts failed.