Batch delete file lists using Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.