# 示例代码:使用 __file__ 获取当前文件的路径
import os
# 获取当前文件的绝对路径
current_file_path = os.path.abspath(__file__)
print(f"当前文件的绝对路径是: {current_file_path}")
# 获取当前文件所在的目录
current_directory = os.path.dirname(os.path.abspath(__file__))
print(f"当前文件所在的目录是: {current_directory}")
__file__
是一个特殊变量,它包含了当前执行脚本的文件名(包括路径)。当我们在模块中使用 __file__
时,它会返回该模块的文件路径。os.path.abspath(__file__)
用于获取当前文件的绝对路径。os.path.dirname()
用于获取文件所在的目录路径。上一篇:python requests库
下一篇:python中class
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站