import os
# 获取当前工作目录的绝对路径
current_directory = os.path.abspath('.')
print(f"当前工作目录的绝对路径是: {current_directory}")
# 获取指定文件的绝对路径
file_path = os.path.abspath('example.txt')
print(f"文件 'example.txt' 的绝对路径是: {file_path}")
# 解释说明:
# os.path.abspath(path) 函数会返回一个文件或目录的绝对路径。
# 如果传入的是相对路径,它会将其转换为绝对路径。
# '.' 表示当前目录,所以 os.path.abspath('.') 返回当前工作目录的绝对路径。
# 对于文件 'example.txt',如果该文件存在于当前工作目录中,则会返回其绝对路径。
上一篇:python运行命令
下一篇:python canvas
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站