使用python读取文件、图像、Excel序列的常用脚本
- 读取excel中的所有列的内容
- 读取文件夹中的所有图像,并获取名称存入img_list
- 统计一个文件夹中所有文件的个数(包含文件本身)
- 将图片批量从a文件夹复制到b文件夹
- 一次性新建多个文件夹
import pandas as pd
import shutil
import os
df = pd.read_excel("./megaage_all.xlsx",names=None)
df_li = df.values.tolist()
result = []
for s_li in df_li:
result.append(s_li[0:2])
img_folder = './MegaAge-Asian'
img_list = [os.path.join(nm) for nm in os.listdir(img_folder) if nm[-3:] in ['jpg','png','gif']]
for i in result:
if i[0] in img_list:
target = os.path.join('age_class',str(i[1]))
path = r'./MegaAge-Asian'
shutil.copyfile(os.path.join('./MegaAge-Asian',i[0]), os.path.join(target,i[0]))
print("完成!",i[0],"的年龄是:",i[1])
for j in range(0,70):
img_folder = os.path.join('age_class',str(j))
print("class_",str(j),":",len(os.listdir(img_folder))-1)
target = r"./b"
path = r'./a'
shutil.copyfile(os.path.join("./a","2.jpg"), os.path.join(target,"2.jpg"))
print("完成!")
import os
path=r'./age_class'
for i in range(0,70):
dir_name=str(i)
os.makedirs(path+'/'+dir_name)