问题1是您定义了一个函数(“def”是“define”的缩写),但您没有调用它。def new_directory(): # define the function
if not os.path.exists(current_sandbox):
os.mkdir(current_sandbox)
new_directory() # call the function
问题2(还没有击中你)是你在使用一个全局(current_sandbox)时应该使用一个参数——在后一种情况下,你的函数通常是有用的,甚至可以从另一个模块调用。问题3是不规则缩进——使用缩进1将导致任何必须阅读您的代码(包括您自己)的人发疯。坚持4,使用空格,而不是制表符。def new_directory(dir_path):
if not os.path.exists(dir_path):
os.mkdir(dir_path)
new_directory(current_sandbox)
# much later
new_directory(some_other_path)