Python batch Delete Files
When writing a program for testing, you will always encounter the log to be deleted from the log folder or delete some historical files, which will be generated each time, during the test, you always need to delete these files before the test to find out the cause of the error. It is troublesome to manually delete the files. Therefore, you need to write a batch Delete script.
Import osdef removeFileInFirstDir (targetDir): for file in OS. listdir (targetDir): targetFile = OS. path. join (targetDir, file) if OS. path. isfile (targetFile): OS. remove (targetFile) lists = ['log', 'downloaded'] if _ name _ = "_ main _": for lista in lists: removeFileInFirstDir (lista)
Put the name of the folder to be deleted into the list, and use the for loop to traverse all the folders to be deleted to delete the files in it.
The script and folder must be at the same level.