Python 您所在的位置:网站首页 索引文件夹中文件的内容 Python

Python

2024-07-06 01:59| 来源: 网络整理| 查看: 265

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:/')

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有