本文共 1880 字,大约阅读时间需要 6 分钟。
from pathlib import Path FILE = Path(__file__).resolve() print('文件路径:', FILE) ROOT = FILE.parents[0] # YOLOv5 的根目录 print('根目录:', ROOT) # 另一种方法 print('当前工作目录:', Path.cwd()) 文件路径:/Users/jjw/Desktop/后端项目/yolov5.6/my_test.py 根目录:/Users/jjw/Desktop/后端项目/yolov5.6 当前工作目录:/Users/jjw/Desktop/后端项目/yolov5.6
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.TXT
from pathlib import Path print(Path('./MaskDataSet/data.yaml').is_file()) # 判断文件是否存在 print(Path('./MaskDataSet').is_file()) # 只有路径但没有文件 print(Path('./MaskDataSet/111.txt').is_file()) # 不存在的文件 True False False
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']
import glob # 获取当前文件下的 runs 文件夹下的所有文件夹 print(glob.iglob('./runs/*')) for i in glob.iglob('./runs/*'): print(i) # 对获取到的文件夹进行排序 print(sorted(glob.iglob('./runs/*'))) ./runs/exp9 ./runs/exp7 ./runs/exp0 ./runs/exp1 ./runs/exp6 ./runs/exp8 ./runs/exp10 ./runs/exp11 ./runs/exp3 ./runs/exp4 ./runs/exp5 ./runs/exp2 ['./runs/exp0', './runs/exp1', './runs/exp10', './runs/exp11', './runs/exp2', './runs/exp3', './runs/exp4', './runs/exp5', './runs/exp6', './runs/exp7', './runs/exp8', './runs/exp9']
from pathlib import Path print(Path("jjw/1.txt").name) 1.txt
转载地址:http://cmafk.baihongyu.com/