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}")
# 获取指定文件的路径
def get_file_path(filename):
return os.path.join(current_directory, filename)
# 示例:获取名为 'example.txt' 的文件路径
example_file_path = get_file_path('example.txt')
print(f"example.txt 文件的路径: {example_file_path}")
os.path.abspath(__file__)
:
os.path.dirname(os.path.abspath(__file__))
:
os.path.join(current_directory, filename)
:
get_file_path(filename)
:
如果你运行这段代码,它会输出当前脚本的路径、所在目录以及 example.txt
文件的路径(假设该文件存在于当前目录中)。
上一篇:python数组转换成字符串
下一篇:python转字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站