Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本
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表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本的更多相关文章
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- python中获取指定目录下所有文件名列表的程序
http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...
- iOS案例:读取指定目录下的文件列表
// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...
- Python读取指定目录下指定后缀文件并保存为docx
最近有个奇葩要求 要项目中的N行代码 申请专利啥的 然后作为程序员当然不能复制粘贴 用代码解决.. 使用python-docx读写docx文件 环境使用python3.6.0 首先pip安装pytho ...
- 遍历并读取指定目录下的所有文件内容,写入Map集合然后输出在控制台和本地文件
public class FileWrite { public static void main(String[] args) throws Exception { //封装数据源目录 File sr ...
- SAS 读取指定目录下文件列表宏
OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...
- TDirectory.GetFiles获取指定目录下的文件
使用函数: System.IOUtils.TDirectory.GetFiles 所有重载: class function GetFiles(const Path: string): TStringD ...
- php获取指定目录下的所有文件列表
在我们实际的开发需求中,经常用到操作文件,今天就讲一下关于获取指定目录下的所有文件的几种常用方法: 1.scandir()函数 scandir() 函数返回指定目录中的文件和目录的数组. scandi ...
- unity 读取excel表 生成asset资源文件
做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...
随机推荐
- mysql资源地址
http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/
- 结构类型:Struct
一.概述: 结构类似于类,但结构为值类型,存储于栈中. 结构不能继承和被继承,但可实现接口. 结构成员访问级别有public,private(默认) ,internal. 1.简单结构 可以将无方法, ...
- Nowcoder Girl 初赛 T5
Nowcoder Girl 初赛第五题 来源 Nowcoder Girl 初赛第五题 题面 \(n(1<=n<=10000)\)件武器,每件武器对于属性有加成,一共五种属性.若使用其中的\ ...
- Windows7下安装配置PostgreSQL10
PostgreSQL安装: 一.windows7下安装过程首先上PostgreSQL官方网站的下载页面https://www.postgresql.org/download/windows/,下载本软 ...
- DateConvertUtil 日期工具类
package com.hxqc.basic.dependency.util; import java.text.DateFormat; import java.text.ParseException ...
- 【HNOI2009】梦幻布丁
题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入输出格式 输入格式 第一 ...
- javascrict中innerhtml和innerText的关系
1.time.innerHTML 就是id为time的标签中所有包含的代码内容 比如 <div id='time'><a href='#'>time</a>< ...
- Java类加载机制与Tomcat类加载器架构
Java类加载机制 类加载器 虚拟机设计团队把类加载阶段中的“通过一个类的全限定名来获取描述此类的二进制字节流”这个动作放到Java虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这 ...
- 404 Note Found 队-Alpha5
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- Ubuntu 编译出现 ISO C++ 2011 不支持的解决办法
问题 在编译时出现如下error: error:This file requires compiler and library support for the ISO C++ 2011 standar ...