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

当前文件的绝对路径:这个就是文件在硬盘上的真实路径,从盘符一直到文件所在的具体位置。

当前工作目录 (current working directory)是文件系统当前所在的目录,如果命令没有额外指定路径,则默认为当前工作目录。

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

 

     

 

参考:

Python 路径[PATH]获取 - 知乎 (zhihu.com)

绝对路径、相对路径、当前路径(当前目录、工作目录)的区分_c++ 工程目录 工作目录 filesystem current_path 区别-CSDN博客

 

posted @ 2023-10-24 17:38  Picassooo  阅读(310)  评论(0编辑  收藏  举报