import pandas as pd
import os
import glob
from pathlib import Path
import shutil
p = Path("C:/Users/Xie ZeYu/Desktop/") #初始化构造Path对象
FileList=list(p.glob("**/*.txt")) #得到该目录下的所有的txt文件
#print (type(FileList))
for filename in FileList:#遍历所有的txt文件名
try:
filename_file = str(filename)
filename_txt = str(filename)
filename_file = filename_file.split(‘.‘)[0]#截取文件夹的目录
os.mkdir(filename_file)#创建文件夹
shutil.move(filename_txt,filename_file)#移动文件
print(filename,‘已完成移动‘)
except Exception as e:
print(‘发现异常‘)
break