python-文件分类

import json
import os
import shutil
import re


def mkdir(path):
    for roots,dirs,names in os.walk(path):
        root = re.findall(r'\d+.\d+.\d',roots)
        if len(root)!=0:
            dirName = ('winxp','win8','win7','win2003','win2008','centos','ubuntu_16_4','debian')
            for name in dirName:
                isExists = os.path.exists(roots+'\\'+name)
                if not isExists:
                    verPath = roots+'\\'+name
                    os.mkdir(verPath)
                    verPaths = verPath.split()
                    print(verPath)

def getPath(path): ##所有网段目录
    paths = list()
    for roots,dirs,files in os.walk(path):
        root = re.findall(r'\d+.\d+.\d',roots)
        if len(root) == 0:
            continue
        else:
            paths.append(roots)
    return paths        
            # return roots
def getName(path): #所有ip文件
    files = list()
    for roots,dirs,names in os.walk(path):
        for name in names:
            name = re.findall(r'\d+.\d+.\d+.\d+.txt',name)
            if len(name) == 0:
                continue
            else:
                files.append(name[0])
    return files
def getVer(file):
    try:
        data = open(file)
        setting = json.load(data)
        Ver = setting['system']
        print (Ver)
        data.close()
        return Ver
    except Exception as e:
        pass
    

def moveFile(path,Ver,name):
    print(path)
    print(Ver)
    print(name)
    try:
        if Ver == 'winxp':
            shutil.move(path+'\\'+name,path+'\\'+'winxp'+'\\'+name)
        elif Ver == 'win8':
            shutil.move(path+'\\'+name,path+'\\'+'win8'+'\\'+name)
        elif Ver == 'win7':
            shutil.move(path+'\\'+name,path+'\\'+'win7'+'\\'+name)
        elif Ver == 'win7-32':
            shutil.move(path+'\\'+name,path+'\\'+'win7'+'\\'+name)
        elif Ver == 'win2003':
            shutil.move(path+'\\'+name,path+'\\'+'win2003'+'\\'+name)
        elif Ver == 'win2008':
            shutil.move(path+'\\'+name,path+'\\'+'win2008'+'\\'+name)
        elif Ver == 'centos':
            shutil.move(path+'\\'+name,path+'\\'+'centos'+'\\'+name)
        elif Ver == 'ubuntu':
            shutil.move(path+'\\'+name,path+'\\'+'ubuntu_16_4'+'\\'+name)
        elif Ver == 'debian':
            shutil.move(path+'\\'+name,path+'\\'+'debian'+'\\'+name)
    except Exception as e:
        pass
if __name__ == '__main__':
    path = "C:\\Users\\cheny\\Desktop\\json_solve\\file"
    # names = getName(path)
    # for name in names:
    #     print(name)
    #     paths = getPath(path)
    #     for path in paths:
    #         print(path)
    #         Ver = getVer(path+'\\'+name)
    #         print(Ver)
    paths = getPath(path)
    for path in paths:
        os.chdir(path)
        mkdir(path)
        names = getName(path)
        for name in names:
            # print(name)
            file_path = path+'\\'+name
            Ver = getVer(path+'\\'+name)
            moveFile(path,Ver,name)
            print(Ver)


















# def mkdir(path):
#     for fpaths,dirs,fs in os.walk(path):
#         path = re.findall(r'\d+.\d+.\d+$',fpaths)
#         if len(path)!=0:
#             dirName = ('winxp','win8','win7','win2003','win2008','centos','ubuntu_16_4','debian')
#             for name in dirName:
#                 isExists = os.path.exists(fpaths+'\\'+name)
#                 if not isExists:
#                     verPath = fpaths+'\\'+name
#                     os.mkdir(verPath)
#                     verPaths = verPath.split()
#                     return verPaths[0]

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 他,曾乘坐水上摩托环绕不列颠群岛;他,曾搭乘小船横越冰冷的北大西洋;他,曾登上冰封万年的珠穆朗玛峰;他,...
    于士淋阅读 641评论 0 1
  • 这是很久以前写的一段文字: 如果可以的话希望你不再拒接我的电话,哪怕回条信息就行, 如果可以的话希望你能常给我打电...
    红色的石头1984阅读 383评论 0 2
  • 卷云空,远山空,南行山水得一空。 风沙送,牛羊送,一别西北,浮生若梦。 纵,纵。
    槚生阅读 350评论 0 1
  • 午夜唰唰似有声 一帘细雨半透明 屋檐珠长穿梭落 蟾跃荷池嬉奏鸣 卧榻无眠忆旧事 詩章有感酿新情 明来红日穿云过 又...
    闲云飘飘阅读 245评论 1 4
  • 《卡桑德拉的困惑》 作者: 安娜•卡敏斯卡翻译 : Phil 但是该如何提醒智者当心他们自己的智慧当心道德家冷漠的...
    Phil0526阅读 673评论 1 7