python_demo_2
如果一个目录不存在,那么自动创建。
import os
MESSAGE = 'The directory already exists'
TESTDIR = 'testdir'
try:
home = os.path.expanduser('~') #通过展开用户的Set home目录来设置变量home
print(home)
# 判断是否存在,不存在创建,存在打印提示信息
if not os.path.exists(os.path.join(home, TESTDIR)):
os.makedirs(os.path.join(home, TESTDIR))
else:
print(MESSAGE)
except Exception as e:
print(e)