我们可以创建一个目录,只有当它不存在时使用
mkdir -p ./my_directory/
我们如何在Python中使用
os.mkdir()
实现同样的效果?
如果 os.mkdir("./my_directory/")
已存在并且 ./my_directory/
不起作用,则 os.mkdir(" -p ./my_directory/")
会失败。
#python program to check if a directory exists
import os
path = "pythonprog"
# Check whether the specified path exists or not
isExist = os.path.exists(path)
if not isExist:
# Create a new directory because it does not exist
os.makedirs(path)
print("The new directory is created!")