Python 配置绝对路径和相对路径(为了解决文件夹内class文件调用别的文件夹内class的问题)
相对路径问题:
import os import sys # sys.path.append(os.getcwd()) # filename = "E:python小脚本测试导入projectsproject1project1_1Text1_1.txt" # 绝对路径 # filename = "E:python小脚本测试导入projectsproject1project1_1Text1_1.txt" # 绝对路径 # filename = "Text1_1.txt" # 当前目录 # filename = "/python小脚本/测试导入/projects/project1/project1_1/Text1_1.txt" # 根目录 # filename = "../测试导入/projects/project1/project1_1/Text1_1.txt" # 上级目录 # filename = "project1_1/Text1_1.txt" # 上级目录 # filename = "../../测试导入/projects/project1/project1_1/Text1_1.txt" # 上级目录的上级目录 # filename = "../../../测试导入/projects/project1/project1_1/Text1_1.txt" # 上级目录的上级目录的上级目录 # filename = "project1_1/Text1_1.txt" # 当前目录下的子目录 filename = "./project1_1/Text1_1.txt" # 当前目录下的子目录,同上 print("当前路径:") print(os.getcwd()) print() with open (filename, encoding="utf-8") as file_object: for line in file_object: print(line.strip())