不闻不若闻之,闻之不若见之,见之不若知之,知之不若行之。——荀子《儒效篇》
常用的解析文件方法
os.path.splitext:分离文件名与扩展名
def _parse_filename(filename): # 解析文件名
"""Parse meta-information from given filename.
Parameters
----------
filename : str
A VeRi image filename.
Returns
-------
(int, int, str, str) | NoneType
Returns a tuple with the following entries:
* Unique ID of the individual in the image
* Index of the camera which has observed the individual
* Filename without extension
* File extension
Returns None if the given filename is not a valid filename.
"""
filename_base, ext = os.path.splitext(filename) # 分离文件名与扩展名
if '.' in filename_base:
# Some images have double filename extensions.
filename_base, ext = os.path.splitext(filename_base)
if ext != ".jpg":
return None
vehicle_id, cam_num, frame_idx, detection_idx = filename_base.split('_')
return int(vehicle_id), int(cam_num[1]), filename_base, ext
os.path.join:路径拼接
os.path.join()函数:连接两个或更多的路径名组件,参数可以传入多个路径
1.如果各组件名首字母不包含’/’,则函数会自动加上
2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃
3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾
import os
print("1:",os.path.join('aaaa','/bbbb','ccccc.txt'))
print("2:",os.path.join('/aaaa','/bbbb','/ccccc.txt'))
print("3:",os.path.join('aaaa','./bbb','ccccc.txt'))
output
1: /bbbb\ccccc.txt
2: /ccccc.txt
3: aaaa\./bbb\ccccc.txt