Batch delete file lists using Python
Environment:
The list of objects to be deleted is known, that is, the objects to be deleted are determined.
The Code is as follows:
#! /Usr/bin/env python
# Coding = UTF-8
# Purpose: to delete a specified file list
Import OS
Import shutil
# Introduction module. OS is a module that includes common operating system functions and shutil is a file operating tool.
Count_not_exist = 0
Count_exist_but_dir = 0
Count_del_file = 0
Backup_dir = '/backup_file /'
Pre_dir_of_file = '/var/www/virtualhost/admin.51auto.cn /'
# Define the variables used
File_object = open ('/tmp/delete.txt ')
# Open a file
For line in file_object.readlines ():
# Reading files
Line = line. rstrip ('\ n ')
# Remove the '\ n' symbol at the end of each line
If OS. path. exists (line ):
# Determining whether the line file or directory exists on the System
If OS. path. isfile (line ):
# Determining line as a file
New_line = line. replace (pre_dir_of_file, backup_dir)
# Replacing the specified directory in line
File_path = OS. path. dirname (new_line)
# Retrieve the directory structure of the new_line File
If not OS. path. exists (file_path ):
# Determine whether the directory structure of the new_line file exists
OS. makedirs (file_path)
# The Directory is not saved. Create a directory.
Print file_path + ': The File Path Create Succeed! '
Shutil. copy2 (line, file_path)
# Back Up Files to the specified Backup Directory
OS. remove (line)
# Deleting an object
Count_del_file + = 1
Else:
Print line + ": It's a directory ."
Count_exist_but_dir + = 1
Else:
Print line + ': The Object is not exists .'
Count_not_exist + = 1
Print str (count_not_exist) + ': The number of objects not exist on the system .'
Print str (count_exist_but_dir) + ": The number of objects exist, but it's directory ."
Print str (count_del_file) + ': The number of objects deleted in right .'
# Print the statistical variable value
File_object.close ()
# Close a file object
This article is from the Focus on the database Blog, please be sure to keep this source http://lgdvsehome.blog.51cto.com/3360656/1241765