1.通过索引excel表将文件进行文件夹分类的脚本,此脚本由于将ip和id对应并生成对应id的文件夹将文件进行分类,也可以任意规定表格内容,通过vul_sc_ip.txt和xlsx文件进行索引。

# -*- coding:utf8 -*-
import sys
import os
import pandas as pd
import shutil
import stat def find(path,ip):
# open the excel file
df = pd.read_excel(path)
if ip in df["ip1"].values:
s1 = df[df["ip1"]==ip]["man"].values.tolist()[0]
return {s1:ip}
else:
return 0
def mkdir(path,filename,desname):
path = path.strip()
path = path.rstrip("\\")
path_e = str(path) + '\\' + filename.decode('utf-8') + '_' + desname
isExists = os.path.exists(path_e)
if not isExists:
os.makedirs(path_e)
print (path_e + ' 创建成功'.decode('utf-8'))
return path_e
else:
print (path_e + ' 目录已存在'.decode('utf-8'))
return path_e def save(rootdir,newpath,ip):
list = os.listdir(rootdir)
ip_name = ip+'.html'
all_path_name = []
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
all_path_name.append(os.path.basename(path))
s_media = os.path.join(rootdir,'media')
d_media = os.path.join(newpath,'media')
isExists_media = os.path.exists(d_media)
if not isExists_media:
shutil.copytree(s_media,d_media)
else:
print (d_media + ' 目录已存在'.decode('utf-8'))
if ip_name in all_path_name:
dst_path = os.path.join(rootdir,ip_name)
newfp = os.path.join(newpath,ip_name)
isExists_newfp = os.path.exists(newfp)
if not isExists_newfp:
shutil.copy(dst_path, newfp)
print ("copy %s -> %s"%(dst_path,newfp))
else:
print (newfp + ' 文件已存在'.decode('utf-8'))
else:
return 0 if __name__ == "__main__": path_report = "D:"#需要分类的文件路径
file_name = "文件名"#每个文件夹的名字
path_xlsx = "D:/ip.xlsx"#索引表路径
host_path = "D:/"#保存文件夹的位置
f = open("vul_sc_ip.txt", 'r')#索引的txt文件,由于此脚本是根据ip索引的所以txt内为IP地址
all_ip = f.readlines()
f.close()
for each_ip in all_ip:
dict_name = find(path_xlsx,each_ip.strip('\n'))
for des_name in dict_name.keys():
new_path = mkdir(path_report,file_name,des_name)
#print new_path
save(host_path,new_path,dict_name.get(des_name))

2.读取指定目录下所有文件名的脚本:

#!/usr/bin/env python
# -*- coding:utf8 -*- import os f = open("ip.txt", 'w+') def file_name(file_dir):
for root, dirs, files in os.walk(file_dir): print >>f,(files) #当前路径下所有非目录子文件 file_name('D:/')

Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本的更多相关文章

  1. python获取指定目录下所有文件名os.walk和os.listdir

    python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...

  2. python中获取指定目录下所有文件名列表的程序

    http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...

  3. iOS案例:读取指定目录下的文件列表

    // // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...

  4. Python读取指定目录下指定后缀文件并保存为docx

    最近有个奇葩要求 要项目中的N行代码 申请专利啥的 然后作为程序员当然不能复制粘贴 用代码解决.. 使用python-docx读写docx文件 环境使用python3.6.0 首先pip安装pytho ...

  5. 遍历并读取指定目录下的所有文件内容,写入Map集合然后输出在控制台和本地文件

    public class FileWrite { public static void main(String[] args) throws Exception { //封装数据源目录 File sr ...

  6. SAS 读取指定目录下文件列表宏

    OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...

  7. TDirectory.GetFiles获取指定目录下的文件

    使用函数: System.IOUtils.TDirectory.GetFiles 所有重载: class function GetFiles(const Path: string): TStringD ...

  8. php获取指定目录下的所有文件列表

    在我们实际的开发需求中,经常用到操作文件,今天就讲一下关于获取指定目录下的所有文件的几种常用方法: 1.scandir()函数 scandir() 函数返回指定目录中的文件和目录的数组. scandi ...

  9. unity 读取excel表 生成asset资源文件

    做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...

随机推荐

  1. C# 平台问题

    最近在C#项目中嵌入一个视频软件Ffplayer,出现报错现象,提示平台开发视频.dll文件的兼容性和加载格式不正确的问题.最终查看是由于项目平台选择的是Any CPU和X86的引起的.目标平台有什么 ...

  2. Visual Studio强行修改运行平台和注意事项

    默认情况下,会发现项目属性中只有一个Any CPU可供选择,无法修改运行平台. 解决方法如下: 右键“解决方案”,选择“属性”,此时发现每一个项目的平台依然只有Any CPU,点击右上角“配置管理器” ...

  3. Shell使用手册

    1.循环数组 list=(20180531 20180430 20180331 20180228 20180131 20171231 20171130 20171031 20170930 201708 ...

  4. 沉淀再出发:jetty的架构和本质

    沉淀再出发:jetty的架构和本质 一.前言 我们在使用Tomcat的时候,总是会想到jetty,这两者的合理选用是和我们项目的类型和大小息息相关的,Tomcat属于比较重量级的容器,通过很多的容器层 ...

  5. 有用的JS函数

    1. QueryString function queryString(key) { var re = new RegExp("[?&]" + key + "=( ...

  6. 【webpack】理解配置文件

    学习链接: http://blog.csdn.net/hongchh/article/details/55113751 https://segmentfault.com/a/1190000009356 ...

  7. git命令将本地代码提交到github

    git命令将本地代码提交到github 步骤: 第一步:进入到相应的文件夹,用git init命令,将该文件夹变成git可管理的仓库 git init 第二步:将项目添加到本地仓库 可以用git st ...

  8. centos 清除yum缓存

    No package nginx available. yum makecache yum install epel-release yum install nginx

  9. PAT——1046. 划拳

    划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...

  10. Selenium & Webdriver 远程测试和多线程并发测试

    Selenium & Webdriver 远程测试和多线程并发测试 Selenium Webdriver自动化测试,初学者可以使用selenium ide录制脚本,然后生成java程序导入ec ...