一、取出当前路径:
BASE_DIR1 = os.path.dirname(os.path.dirname(__file__))
BASE_DIR2 = os.path.dirname(os.path.abspath(__file__))
# __file__:是指这个文件所在的目录地址(此地址里包括文件名),但跟执行python命令所在目录有关(相对地址)。
# dirname(__file__):文件的上一级目录。
# abspath(__file__):文件所在的绝对路径(此地址里包括文件名)。
二、判断路径是否存在
(1)判断文件夹是否存在
os.path.exists(path)
(2)判断文件是否存在
os.path.isfile('a.txt') #如果不存在就返回False
os.path.exists(directory) #如果目录不存在就返回False
(3)创建文件夹
os.makedirs(path) #多层创建目录
os.mkdir(path) #创建目录