The following is an example of how to delete files and folders under a certain path in Python. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together
Python script to delete files and folders under a certain path
#!/usr/bin/env python import os import shutil delList = [] delDir = "/home/test" delList = os.listdir(delDir ) for f in delList: filePath = os.path.join( delDir, f ) if os.path.isfile(filePath): os.remove(filePath) print filePath + " was removed!" elif os.path.isdir(filePath): shutil.rmtree(filePath,True) print "Directory: " + filePath +" was removed!"
shutil is a high-level file operation module. The True parameter indicates ignore_errors (ignore errors during copying).
is similar to the advanced API, and its main strength is that it supports file copying and deletion operations better.
[Attachment] Usage of os module (library)
Functions such as searching and deleting folders and files are implemented in the os module.
1. Get the current directory
#1.1 s = os.getcwd() # s 中保存的是当前的执行目录(即执行所在的文件夹)
[Note ]
If you want to get the location of the current directory where the program is running, you can use the os.getcwd() function of the os module.
If you want to get the directory location of the currently executed script, you need to use the sys.path[0] variable of the sys module or sys.argv[0] to get it
#1.2 import os import time folder = time.strftime(r"%Y-%m-%d_%H-%M-%S",time.localtime()) os.makedirs(r'%s/%s'%(os.getcwd(),folder))
2. Change the current directory
##
os.chdir( "/root/123") #将当前目录设为 "/root/123" #说明: 当指定的目录不存在时,引发异常。
3. Decompose a path name into two parts: directory name and file name
fpath , fname = os.path.split( "你要分解的路径")
a, b = os.path.split( "/root/123/test.txt" ) print a print b
/root/123/ test.txt
##4. Decompose the extension of the file name
fpathandname , fext = os.path.splitext( "你要分解的路径")
a, b = os.path.splitext( "/root/123/test.txt" ) print a print b
/root/123/test .txt
5. Determine whether a path (directory or file) exists
b = os.path.exists( "你要判断的路径")
Return value b: True or False
6. Determine whether a path is a file
b = os.path.isfile( "你要判断的路径")
Return value b: True or False
7. Determine whether a path is a directory
b = os.path.isdir( "你要判断的路径")
Return value b: True or False
8. Get the files and subtitles in a directory List of directories
L = os.listdir( "你要判断的路径")
L = os.listdir( "/root/123" ) print L
[‘test.txt', ‘test.py','python'] #这里面既有文件也有子目录
os.makedirs( path ) # path 是"要创建的子目录"
For example:
os.makedirs("/root/123")
(1) When path already exists (whether it is a file or a folder)
(2) The drive does not exist (3) The disk is full(4) The disk is read-only or does not have write permission10. Delete subdirectoriesos.rmdir( path ) # path: "Subdirectory to be deleted"
Possible reasons for exception:
( 1) path does not exist
(2) There are files or subordinate subdirectories in the path subdirectory(3) No operation permission or read-only11. Delete filesos.remove( filename ) # filename: "要删除的文件名"
os.name( oldfileName, newFilename)
Cause of exception:
(1) oldfilename The old file name does not exist
(2) newFilename When the new file already exists, at this time, you need to delete the newFilename file first. Related recommendations:
python Delete files before the specified time interval
Unzip the zip file under python and delete the file Example of _python
The above is the detailed content of Python implements deleting files and folders under a certain path. For more information, please follow other related articles on the PHP Chinese website!