from pathlib import Path FILE = Path(__file__).resolve() print('FILEn', FILE) ROOT = FILE.parents[0] # YOLOv5 root directory print('rootn', ROOT) # 另一种方法 print('Path.cwd()n', Path.cwd())执行结果:
FILE /Users/jjw/Desktop/后端项目/yolov5.6/my_test.py root /Users/jjw/Desktop/后端项目/yolov5.6 Path.cwd() /Users/jjw/Desktop/后端项目/yolov5.62、获取文件的后缀并用小写表示
from pathlib import Path print(Path('jjw/hello/1.txt').suffix.lower()) print(Path('/jjw/hello/1.txt').suffix.lower()) print(Path('/jjw/hello/1.TXT').suffix)执行结果:
.txt .txt .TXT3、判断文件是否存在
from pathlib import Path # 判断给定的文件是否存在 ./MaskDataSet/data.yaml 正确的路径所以打印True print(Path('./MaskDataSet/data.yaml').is_file()) # 只有路径没有文件所以打印FALSE print(Path('./MaskDataSet').is_file()) # 不存在的文件,所以打印FALSE print(Path('./MaskDataSet/111.txt').is_file())打印结果
True False False4、获取文件夹并对其进行路径进行排序
import glob # 获取当前文件下的runs文件夹下的所有文件夹 print(glob.glob('./runs/*')) # 对获取到的文件夹进行排序 print(sorted(glob.glob('./runs/*')))运行结果:
['./runs/exp0', './runs/exp1', './runs/exp6', './runs/exp3', './runs/exp4', './runs/exp5', './runs/exp2'] ['./runs/exp0', './runs/exp1', './runs/exp2', './runs/exp3', './runs/exp4', './runs/exp5', './runs/exp6']
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)