Python classifies various files in a txt document according to their extension

It is necessary to classify many file names of a txt file according to the path. If the number is small, it can be done manually. If there are many numbers and categories, it is not applicable. Here, python code is used to classify.
I built a txt file for testing, and there are some file paths: number of files to
Insert picture description here
read:

import os
path='D:/Python_Program/test/test.txt'
with open(path) as ff:
    tmp = ff.readlines()
print("一共含有{}个文件".format(len(tmp)))
一共含有55个文件

Sort and write a new file:

# 根据扩展名分类并写入文件中
outpath='D:/Python_Program/test/output'
print("一共含有{}个元素".format(len(tmp)))
for x in tmp:
    xx=x.strip()  # 去掉每一行后面的回车,否则后面会出错
    filepath,filename=os.path.split(xx)
    print(filepath,'-------',filename)
    houzhui=filename.split(".") #判断是否有后缀

    if len(houzhui)==1:  #没有后缀的文件名放入一个文件
        print("没有后缀:",filename)
        with open(os.path.join(outpath,'binda.txt'),'a+') as fw:
            fw.write(x)
            print("写入文件binda.txt   -----")

    else: # 有后缀的时候根据后缀名分类
        zi = []
        houzhuiming = filename.split(".")[-1] # 获取文件扩展名
        if houzhuiming not in zi: # 如果扩展名没有出现过
            # zi.append(houzhuiming)
            print("houzhuiming:",houzhuiming)

            ffname = houzhuiming + ".txt"

            with open(os.path.join(outpath,ffname),'a+') as fw:
                print("写入文件{}".format(ffname),"-----")
                fw.write(x)
        else:  #如果扩展名出现过,则放入相应类别
            ffname = houzhuiming + ".txt"
            with open(os.path.join(outpath, ffname), 'a+') as fw:
                print("写入文件{}".format(ffname),"-----")
                fw.write(x)


There will be corresponding files in the specified folder:
Insert picture description here

Then test it:

# 测试用,检查分类后的文件数是否和分类前相同
outpath='D:/Python_Program/test/output'
files=os.listdir(outpath)
allfile=[]
for fi in files:
    with open(os.path.join(outpath,fi)) as ff:
        tmp=ff.readlines()
        allfile+=tmp
print("分类后含有{}个文件".format(len(allfile)))
分类后含有50个文件

When writing a new file, you need to pay attention, you cannot use'w', because'w' will be overwritten, here to verify:
Insert picture description here

# 根据扩展名分类并写入文件中
outpath='D:/Python_Program/test/output'
print("一共含有{}个元素".format(len(tmp)))
for x in tmp:
    xx=x.strip()  # 去掉每一行后面的回车,否则后面会出错
    filepath,filename=os.path.split(xx)
    print(filepath,'-------',filename)
    houzhui=filename.split(".") #判断是否有后缀

    if len(houzhui)==1:  #没有后缀的文件名放入一个文件
        print("没有后缀:",filename)
        with open(os.path.join(outpath,'binda.txt'),'w') as fw:
            fw.write(x)
            print("写入文件binda.txt   -----")

    else: # 有后缀的时候根据后缀名分类
        zi = []
        houzhuiming = filename.split(".")[-1] # 获取文件扩展名
        if houzhuiming not in zi: # 如果扩展名没有出现过
            # zi.append(houzhuiming)
            print("houzhuiming:",houzhuiming)

            ffname = houzhuiming + ".txt"

            with open(os.path.join(outpath,ffname),'w') as fw:
                print("写入文件{}".format(ffname),"-----")
                fw.write(x)
        else:  #如果扩展名出现过,则放入相应类别
            ffname = houzhuiming + ".txt"
            with open(os.path.join(outpath, ffname), 'w') as fw:
                print("写入文件{}".format(ffname),"-----")
                fw.write(x)


# 测试用,检查分类后的文件数是否和分类前相同
outpath='D:/Python_Program/test/output'
files=os.listdir(outpath)
allfile=[]
for fi in files:
    with open(os.path.join(outpath,fi)) as ff:
        tmp=ff.readlines()
        allfile+=tmp
print("分类后含有{}个文件".format(len(allfile)))
分类后含有8个文件

Guess you like

Origin blog.csdn.net/liulanba/article/details/110870879