import os current_dir = os.path.dirname(__file__) print("当前文件的目录是:", current_dir) current_path = os.path.abspath(__file__) print("当前文件的路径是:", current_path)
运行结果:
当前文件的目录是: d:\abc 当前文件的路径是: d:\abc\test.py