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): ##所有網(wǎng)段目錄
    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]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 他,曾乘坐水上摩托環(huán)繞不列顛群島;他,曾搭乘小船橫越冰冷的北大西洋;他,曾登上冰封萬(wàn)年的珠穆朗瑪峰;他,...
    于士淋閱讀 681評(píng)論 0 1
  • 這是很久以前寫的一段文字: 如果可以的話希望你不再拒接我的電話,哪怕回條信息就行, 如果可以的話希望你能常給我打電...
    紅色的石頭1984閱讀 418評(píng)論 0 2
  • 卷云空,遠(yuǎn)山空,南行山水得一空。 風(fēng)沙送,牛羊送,一別西北,浮生若夢(mèng)。 縱,縱。
    槚生閱讀 386評(píng)論 0 1
  • 午夜唰唰似有聲 一簾細(xì)雨半透明 屋檐珠長(zhǎng)穿梭落 蟾躍荷池嬉奏鳴 臥榻無(wú)眠憶舊事 詩(shī)章有感釀新情 明來(lái)紅日穿云過(guò) 又...
    閑云飄飄閱讀 289評(píng)論 1 4
  • 《卡桑德拉的困惑》 作者: 安娜?卡敏斯卡翻譯 : Phil 但是該如何提醒智者當(dāng)心他們自己的智慧當(dāng)心道德家冷漠的...
    Phil0526閱讀 768評(píng)論 1 7

友情鏈接更多精彩內(nèi)容