Python中os.path和os.makedirs的运用(判断文件或文件夹是否存在,创建文件夹)

import os
import numpy as np

data = np.array([1, 2, 3])
if not os.path.exists("./data/"):
    print("# path not exists")
    os.makedirs("./data/")
    if not os.path.exists("./data/data.npy"):
        print("# data.npy not exists")
        np.save("./data/data.npy", data)

print("# path exists? :", os.path.exists("./data/"))
print("# data.npy exists? :", os.path.exists("./data/data.npy"))

运行结果:

# path not exists
# data.npy not exists
# path exists? : True
# data.npy exists? : True

 

你可能感兴趣的:(Python)