zoukankan      html  css  js  c++  java
  • 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('
    '))
            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:/')
  • 相关阅读:
    Silverlight实用窍门系列:71.Silverlight的Style
    Silverlight实用窍门系列:70.Silverlight的视觉状态组VisualStateGroup
    PostSharp AOP编程:5.PostSharp的MethodInterceptionAspect类基本组成
    Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
    Silverlight实用窍门系列:67.Silverlight下的Socket通讯
    使用Gitorious搭建私有代码仓库
    解决Rabbitmqserver 启动失败
    搭建代码审查系统Gerrit
    [译]5.9. Virtual Devices 虚拟设备
    [译]5.7. Initializing the Device Handling Layer: net_dev_init 设备处理层的初始化:net_dev_init
  • 原文地址:https://www.cnblogs.com/pureqh/p/10758593.html
Copyright © 2011-2022 走看看