python操作上级子文件
.
└── folder
├── data
│ └── data.txt
└── test1
└── test2
└── test.py
import os
'***获取当前目录***'
print os.getcwd()
print os.path.abspath(os.path.dirname(__file__))
***获取上级目录***'
print os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
print os.path.abspath(os.path.dirname(os.getcwd()))
print os.path.abspath(os.path.join(os.getcwd(), ".."))
'***获取上上级目录***'
print os.path.abspath(os.path.join(os.getcwd(), "../.."))
---------------------
windows系统,实例:
#假设当前在test.py,找到data.txt路径
parent_path = os.path.abspath(os.path.join(os.getcwd(), "../.."))
data_dir = os.path.join(parent_path, 'data\\data.txt')