SoFunction
Updated on 2024-11-15

The python implementation for deleting files and emptying directories

Python () method

() method is used to delete a file at the specified path. If the specified path is a directory, OSError is thrown.

Works in Unix, Windows

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# List directories
print "The catalog is: %s" %(())

# Remove
("")

# List catalogs after removal
print "after removal : %s" %(())

The output of executing the above program is:

Cataloged as.
[ '','','' ]
After removal.
[ '','' ]

Python () method

() method is used to recursively remove directories. Like rmdir(), if the subfolders are deleted successfully, removedirs() tries their parent folders until it throws an error (which is basically ignored, since it generally means that your folder is not empty).

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# List directories
print "The catalog is: %s" %(())

# Remove
("/test")

# List the removed directories
print "Directory after removal is:" %(())

The output of executing the above program is:

Cataloged as.
[  '','','','test' ]
The removed directory reads.
[  '','','' ]

Python () method

() method is used to delete a directory at the specified path. Only if the folder is empty, otherwise, OSError is thrown.

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# List directories
print "The catalog is: %s"%(())

# Delete path
("mydir")

# List the renamed directories
print "The catalog is: %s" %(()) 

The output of executing the above program is:

Cataloged as.
[  '','','','mydir' ]
Cataloged as.
[  '','','' ]

Python () method

() method is used to delete a file, and returns an error if the file is a directory.

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# List directories
print "The catalog is: %s" %(())

("")

# Directory after deletion
print "The deleted directory is : %s" %(()) 

The output of executing the above program is:

Cataloged as.
[ '','','']
The deleted directory is :
[ '','' ]

Other summaries

1. remove() is the same as unlink().

In Windows, deleting a file in use throws an exception. In Unix, the records in the directory table are deleted, but the storage of the file remains.

# Delete files using () and ()
#!/user/local/bin/python2.7
# -*- coding:utf-8 -*-
import os
my_file = 'D:/'
if (my_file):
 # Delete the file, the following two methods can be used.
 (my_file)
 #(my_file)
else:
 print 'no such file:%s'%my_file 

2, recursive method of deleting directories and files (similar to the DOS command DeleteTree):

The code is as follows.

import os
for root, dirs, files in (top, topdown=False):
 for name in files:
  ((root, name))
 for name in dirs:
  ((root, name)) 

3, Python clear all the files in the specified folder:

This requirement is very simple: you need to empty the specified folder before executing certain code, if you use () directly, you may not be able to delete the folder because the files in the folder are occupied, the solution is also very simple, first delete the folder forcibly, and then rebuild the folder with the same name:

import shutil 
('The name of the folder to be emptied') 
('The name of the folder to be emptied') 

Note: See the introduction to the shutil module here:https:///article/

If you want to move a file from one folder to another and rename it at the same time, it's also easy with shutil:

('Original folder/original file name','Target folder/target file name')

4. python delete non-empty folder

Generally, when deleting a file, you can use os library, and then use (path) to complete the deletion, and if you delete an empty folder, you can use (path) to complete the deletion.
But if you need to delete the entire folder, and the folder is not empty when the use of (path) will be an error, this time you can use shutil library, the library for python built-in library, is a file and folder advanced operations library, can be complemented with os libraries to complete some of the operations, such as folder copy the whole folder, move the folder, rename the file and so on.

import os
import shutil
(path) # Delete the file
(path) # Delete empty folders
(path) # Delete folders recursively

References:

1、/muwinter/article/details/77196261 2018.5.25
 2、/qysh123/article/details/51923606 2018.5.25
 3、/python/ 2018.5.25
 4、/python/ 2018.5.25
 5、/python/ 2018.5.25
 6、/python/ 2018.5.25

to this article on python delete files, empty directory implementation method is introduced to this article, more related python delete files, empty directory content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!