你可以使用os.path模块来获取指定文件的路径。以下是一个示例代码:
import os# 获取当前文件的路径current_path = os.path.abspath(__file__)print("当前文件的路径:", current_path)# 获取当前文件所在的目录current_dir = os.path.dirname(current_path)print("当前文件所在的目录:", current_dir)# 获取指定文件的路径file_path = os.path.join(current_dir, "filename.txt")print("指定文件的路径:", file_path)在这个示例中,os.path.abspath(__file__)用于获取当前文件的绝对路径,os.path.dirname(current_path)用于获取当前文件所在的目录,而os.path.join(current_dir, "filename.txt")用于将当前目录和指定的文件名拼接成完整的路径。

