郑州微网站开发,纪念馆网站建设方案,小说网站排名免费,荆门网站建设514885使用Python批量删除文件列表环境#xff1a;已知要删除的文件列表#xff0c;即确定哪些文件要删除。代码如下#xff1a;#!/usr/bin/env python#codingutf-8#目的#xff1a;本程序主要为删除给定的文件列表import osimport shutil#引入模块#xff0c;os为包含普遍的操作…使用Python批量删除文件列表环境已知要删除的文件列表即确定哪些文件要删除。代码如下#!/usr/bin/env python#codingutf-8#目的本程序主要为删除给定的文件列表import osimport shutil#引入模块os为包含普遍的操作系统功能shutil为文件操作工具的模块count_not_exist 0count_exist_but_dir 0count_del_file 0backup_dir /backup_file/pre_dir_of_file /var/www/virtualhost/admin.51auto.cn/#定义所使用的变量file_object open(/tmp/delete.txt)#打开文件for line in file_object.readlines():#读取文件line line.rstrip(\n)#去除每行末尾的\n符号if os.path.exists(line):#判定line文件或目录在系统上存在if os.path.isfile(line):#判定line为文件new_line line.replace(pre_dir_of_file,backup_dir)#替换line中指定目录部分file_path os.path.dirname(new_line)#取出new_line文件的目录结构if not os.path.exists(file_path):#判定new_line文件的目录结构是否存在os.makedirs(file_path)#其目录不存创建目录print file_path :The File Path Create Succeed!shutil.copy2(line,file_path)#将文件备份到指定的备份目录os.remove(line)#删除文件count_del_file 1else:print line :Its a directory.count_exist_but_dir 1else:print line :The Object is not exists.count_not_exist 1print 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 its directory.print str(count_del_file) :The number of objects deleted in right.#打印统计变量的值file_object.close()#关闭文件对象