import os
# 获取当前脚本的绝对路径
current_file_path = os.path.abspath(__file__)
print(f"当前脚本的绝对路径: {current_file_path}")
# 获取当前工作目录的绝对路径
current_working_directory = os.getcwd()
print(f"当前工作目录的绝对路径: {current_working_directory}")
# 获取指定文件或目录的绝对路径
specified_path = os.path.abspath("example.txt")
print(f"指定文件或目录的绝对路径: {specified_path}")
os.path.abspath(__file__):
__file__ 是一个特殊的变量,它包含了当前 Python 脚本的文件名。os.path.abspath() 函数将返回该文件的绝对路径。os.getcwd():
os.getcwd() 函数返回当前工作目录的绝对路径。当前工作目录是指你运行 Python 脚本时所在的目录。os.path.abspath("example.txt"):
"example.txt"),它会将其转换为绝对路径。希望这些代码和解释对你有帮助!
上一篇:python 结束进程
下一篇:python添加路径
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站