使用 Path 对象来定义路径
1.relative_to
方法用于获取一个路径相对于另一个路径的相对路径。下面是一个详细的示例,帮助你更好地理解 relative_to
的用法。
示例
假设我们有以下路径结构:
base_dir/
subdir1/
file1.txt
subdir2/
file2.txt
代码示例
from pathlib import Path
# 定义基础路径
base_dir = Path('/path/to/base_dir')
# 定义子路径
file1_path = Path('/path/to/base_dir/subdir1/file1.txt')
file2_path = Path('/path/to/base_dir/subdir2/file2.txt')
# 获取相对路径
relative_file1_path = file1_path.relative_to(base_dir)
relative_file2_path = file2_path.relative_to(base_dir)
print(f"Relative path of file1: {relative_file1_path}")
print(f"Relative path of file2: {relative_file2_path}")
输出
Relative path of file1: subdir1/file1.txt
Relative path of file2: subdir2/file2.txt
解释
- 定义基础路径:
base_dir
是基础路径。 - 定义子路径:
file1_path
和file2_path
是具体的文件路径。 - 获取相对路径:
file1_path.relative_to(base_dir)
返回file1.txt
相对于base_dir
的相对路径,即subdir1/file1.txt
。file2_path.relative_to(base_dir)
返回file2.txt
相对于base_dir
的相对路径,即subdir2/file2.txt
。
应用
在下面脚本中,relative_to
方法用于获取 .jpg.json
文件相对于 path_all0
的相对路径。然后,你可以使用这个相对路径来构建目标图片的路径。
import os
import glob
from pathlib import Path
from tqdm import tqdm
# 定义路径
path_all0 = Path(r'/pre_label/Sity_20240326_temp/sity_latest/')
img_base_path = Path(r'/train_data/sity/Aty/pre_label/v1/')
out_path = Path(r"/yolo2025\train\train.txt").resolve()
# 获取所有 .jpg.json 文件
path_img_f = list(glob.glob(f"{path_all0}/**/*.jpg.json", recursive=True))
# 写入文件
with open(out_path, "w") as file:
for j in tqdm(path_img_f, desc="***数据处理进度***"):
# 使用 os.path 模块处理路径
relative_path = Path(j).relative_to(path_all0)
img_path = img_base_path / relative_path.with_suffix('.jpg')
# 检查文件存在性
if img_path.exists() and Path(j).exists():
file.write(f"{img_path}\n")
else:
print(f"文件不存在: {img_path} 或 {j}")
解释
- 定义路径:使用
Path
对象定义基础路径和目标路径。 - 获取文件列表:使用
glob
获取所有.jpg.json
文件。 - 路径处理:
relative_path = Path(j).relative_to(path_all0)
:获取j
相对于path_all0
的相对路径。img_path = img_base_path / relative_path.with_suffix('.jpg')
:将.jpg.json
文件的后缀替换为.jpg
,并拼接成目标图片路径。
- 文件存在性检查:在写入文件之前,确保目标文件和对应的图片文件都存在。如果文件不存在,打印一条调试信息。
2.with_suffix
方法是 Path
对象的一个方法,用于更改路径的文件扩展名。下面是一个详细的示例,帮助你更好地理解 with_suffix
的用法。
示例
假设我们有以下路径:
/path/to/file1.txt
我们想将这个路径的扩展名从 .txt
改为 .jpg
。
代码示例
from pathlib import Path
# 定义路径
file_path = Path('/path/to/file1.txt')
# 更改文件扩展名
new_file_path = file_path.with_suffix('.jpg')
print(f"Original path: {file_path}")
print(f"New path: {new_file_path}")
输出
Original path: /path/to/file1.txt
New path: /path/to/file1.jpg
解释
- 定义路径:
file_path
是原始路径。 - 更改文件扩展名:
file_path.with_suffix('.jpg')
返回一个新的Path
对象,其文件扩展名被更改为.jpg
。
- 输出路径:打印原始路径和新路径。
应用
在如下脚本中,with_suffix
方法用于将 .jpg.json
文件的扩展名更改为 .jpg
。然后,你可以使用这个新的路径来构建目标图片的路径。
优化后的脚本
import os
import glob
from pathlib import Path
from tqdm import tqdm
# 定义路径
path_all0 = Path(r'/pre_label/Sity_20240326_temp/sity_latest/')
img_base_path = Path(r'/train_data/sity/Aty/pre_label/v1/')
out_path = Path(r"/yolo_2025\train\train.txt").resolve()
# 获取所有 .jpg.json 文件
path_img_f = list(glob.glob(f"{path_all0}/**/*.jpg.json", recursive=True))
# 写入文件
with open(out_path, "w") as file:
for j in tqdm(path_img_f, desc="***数据处理进度***"):
# 使用 os.path 模块处理路径
relative_path = Path(j).relative_to(path_all0)
img_path = img_base_path / relative_path.with_suffix('.jpg')
# 检查文件存在性
if img_path.exists() and Path(j).exists():
file.write(f"{img_path}\n")
else:
print(f"文件不存在: {img_path} 或 {j}")
解释
- 定义路径:使用
Path
对象定义基础路径和目标路径。 - 获取文件列表:使用
glob
获取所有.jpg.json
文件。 - 路径处理:
relative_path = Path(j).relative_to(path_all0)
:获取j
相对于path_all0
的相对路径。img_path = img_base_path / relative_path.with_suffix('.jpg')
:将.jpg.json
文件的后缀替换为.jpg
,并拼接成目标图片路径。
- 文件存在性检查:在写入文件之前,确保目标文件和对应的图片文件都存在。如果文件不存在,打印一条调试信息。