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. Java学习---Java面试基础考核·

    Java中sleep和wait的区别 ① 这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线 ...

  2. lua垃圾回收之空表

    故事背景: 自己手动手写的一个lua外部库luaopen_xxx,采用了tolua++1.0.93,编译后得到xxx.dll,当在luajit中require 'xxx'后是正常的,但如果运行环境换成 ...

  3. [EffectiveC++]item13:Use objects to manage resources(RAII)

    baidu百科 RAII 百科名片 RAII,也称为“资源获取就是初始化”,是c++等编程语言常用的管理资源.避免内存泄露的方法.它保证在任何情况下,使用对象时先构造对象,最后析构对象. 目录 RAI ...

  4. HTTP协议图--HTTP 响应状态码(重点分析)

    1. 状态码概述 HTTP 状态码负责表示客户端 HTTP 请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作. HTTP 状态码如 200 OK ,以 3 位数字和原因短语组成.数字中 ...

  5. working-with-php-and-beanstalkd

    原文出处:http://www.lornajane.net/posts/2014/working-with-php-and-beanstalkd Working with PHP and Beanst ...

  6. BZOJ3224:普通平衡树(Splay)

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...

  7. 【node.js】事件循环、EventEmitter

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. 事件驱动程序 ...

  8. 2019.3.6 Github学习 &Git学习

    Github学习 1.使用Github 1.1 目的 借助github托管项目代码 1.2 基本概念 仓库(Repository) 仓库的意思是:即你的项目,你想在Github上开源一个项目,那就必须 ...

  9. matplotlib.pyplot的两种模式

    在交互模式下: 1.plt.plot(x)或plt.imshow(x)是直接出图像,不需要plt.show() 2.如果在脚本中使用ion()命令开启了交互模式,没有使用ioff()关闭的话,则图像会 ...

  10. nRF5 SDK for Mesh(五) Light switch demo 点灯例子

    Light switch demo  灯开demo   Purpose This demo project consists of four sub examples - The light swit ...