import os
# 获取当前脚本的绝对路径
script_path = os.path.abspath(__file__)
# 获取当前脚本所在的目录
script_dir = os.path.dirname(script_path)
print("当前脚本的路径是:", script_path)
print("当前脚本所在的目录是:", script_dir)
os.path.abspath(__file__)
:获取当前脚本的绝对路径。__file__
是一个特殊变量,它包含了当前脚本的文件名(包括路径)。os.path.dirname(script_path)
:从脚本的绝对路径中提取出目录部分,即当前脚本所在的目录。print
:输出当前脚本的路径和所在目录。这段代码可以帮助你获取并打印当前 Python 脚本的完整路径及其所在的目录。
上一篇:python os.chdir
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站