使用Python批量删除文件列表 推荐
2013-07-04 18:40
190 查看
使用Python批量删除文件列表
环境:
已知要删除的文件列表,即确定哪些文件要删除。
代码如下:
#!/usr/bin/env python
#coding=utf-8
#目的:本程序主要为删除给定的文件列表
import os
import shutil
#引入模块,os为包含普遍的操作系统功能;shutil为文件操作工具的模块
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/'
#定义所使用的变量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 += 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.'
#打印统计变量的值
file_object.close()
#关闭文件对象
环境:
已知要删除的文件列表,即确定哪些文件要删除。
代码如下:
#!/usr/bin/env python
#coding=utf-8
#目的:本程序主要为删除给定的文件列表
import os
import shutil
#引入模块,os为包含普遍的操作系统功能;shutil为文件操作工具的模块
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/'
#定义所使用的变量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 += 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.'
#打印统计变量的值
file_object.close()
#关闭文件对象
相关文章推荐
- Windows 7 删除文件打开方式列表 使用注册表
- 使用Python批量删除文件
- Win7系统使用ftp的dir或ls命令无法查看文件列表
- java io 使用递归删除指定的文件夹及其子文件夹和文件 先测试再正式使用哦
- 初次使用 git 的“核弹级选项”:filter-branch 从仓库中删除文件
- 使用SVNkit删除版本库的文件
- 使用Python批量替换指定目录所有文件中的指定文本
- 如何使用python递归查找并删除某个目录下的文件
- 如何删除正在使用中的dll文件
- Win7旗舰版使用系统自带功能找回删除的文件教程
- 注册表添加和删除文件打开方式列表
- c#使用filesystemwatcher实时监控文件目录的添加和删除
- java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~
- 使用Ext的Grid,Form,Dialog来实现分页列表,创建,修改,删除功能
- [导入].net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- bat使用forfiles自动批量删除过期文件
- 嵌入式Linux下使用crond服务(定期删除过期文件)
- C#实现Ftp各种操作(上传,下载,删除文件,创建目录,删除目录,获得文件列表)
- 使用lsof恢复rm -rf删除后的文件
- 在使用linux的过程中,难免会误删除一些文件