面试题答案
一键面试import os
# 使用相对路径读取test.txt文件内容
relative_path = os.path.join('project','sub_dir', 'test.txt')
try:
with open(relative_path, 'r') as file:
content = file.read()
print(content)
except FileNotFoundError:
print("文件未找到")
# 将相对路径转换为绝对路径
absolute_path = os.path.abspath(relative_path)
print("绝对路径:", absolute_path)