python当前工作目录和当前文件的绝对路径

发布时间 2023-10-24 17:43:17作者: Picassooo
import os

# 当前文件的绝对路径
print(os.path.abspath(__file__))  # 输出:/home/wp/st_detection/download_code/YOLOv5/ultralytics_yolov5_master/train.py

# 当前工作目录
print(os.path.abspath('.'))       # 输出:/home/wp/st_detection/download_code/YOLOv5
print(os.getcwd())                # 输出:/home/wp/st_detection/download_code/YOLOv5

# 更改当前工作目标
os.chdir('/home/wp/st_detection/download_code/TubeR')
print(os.getcwd())                # 输出:/home/wp/st_detection/download_code/TubeR