import os
path = "D:/dataset"
# 如果路径不存在则创建
if not os.path.exists(path):
os.makedirs(path)
import os
print(os.getcwd())
os库
# 导入系统模块
import os
# 打印当前路径下所有文件名
folder = './'
filelist = os.listdir(folder)
print(filelist)
# 要查看的文件夹
# 文件路径注意一下:windows用 / 或 \\ 代替 \,否则会转义,路径前加r防止转义
folder = r'D:\文件夹\图片'
# 打印文件夹里所有文件名
filelist = os.listdir(folder)
print(filelist)
# 逐个打印含有S0的文件名
filelist = os.listdir(folder)
for filename in filelist:
if "S0" in filename:
print(filename)
glob库
import glob
# 使用glob库打印路径时要加 * , 但使用os库时不用
path_list = glob.glob(r'data_train\*')
for filepath in path_list:
print(filepath)
# 输出的是完整路径,如['data_train\\1.png']
将eyu.png修改为pig.bmp
import os
folder = r'D:\文件夹\图片'
filelist = os.listdir(folder)
for filename in filelist:
if 'eyu' in filename:
os.rename(filename, "pig.bmp")
# 按"."划分文件名
filename = 'eyu.jpg
name = filename.split(".")
# ['eyu','jpg']
eyu = name[0]
suffix = name[1]
import cv2 as cv
imgpath = r"D:\imgs\bird.png"
# 读取图片,结果img为numpy数组
img = cv.imread(imgpath)
# print(img.shape)
# (512,512,3),height, width, 3通道
import cv2 as cv
#把img保存成图片
filename = "D:/imgs/eyu.jpg"
cv.imwrite(filename,img)