setup.py
import os
import shutil
import config
# 创建路径,先破后立
def mkdir(path):
if os.path.exists(path):
shutil.rmtree(path)
os.mkdir(path)
if __name__ == '__main__':
# save fine-tune data
mkdir(config.FINE_TUNE_DATA)
# save pre-train model
mkdir(config.SAVE_MODEL_PATH.strip().rsplit('/', 1)[0])
# save fine-tune model
mkdir(config.FINE_TUNE_MODEL_PATH.strip().rsplit('/', 1)[0])
config.py
FINE_TUNE_DATA = './data_set'
SAVE_MODEL_PATH = './pre_train_model/model_save.model'
FINE_TUNE_MODEL_PATH = './fine_tune_model/fine_tune_model_save.model'
注意到 SAVE_MODEL_PATH 和 FINE_TUNE_MODEL_PATH 都是文件路径,而不是文件夹路径。rsplit('/', 1)[0]
从右边按照 '/' 分割一次,再取第1个元素,就是文件所在目录了。
a = './fine_tune_model/fine_tune_model_save.model'
print(a.rsplit('/', 1))
['./fine_tune_model', 'fine_tune_model_save.model']