感谢@James,这个问题彻底解决了!
解决方案 创建一个特定的函数来创建目录,如下所示:
# SETTINGS.PY
pathNAME = '../Location'
PathName2 = pathNAME + '/Location2'
# Define Functions
def make_dir(directoryName):
if os.path.exists(directoryName):
if os.path.isdir(directoryName):
return False
os.mkdir(directoryName)
print(directoryName)
return True
然后在主脚本
import settings
settings.make_dir(settings.pathNAME)
settings.make_dir(settings.pathName2)
你就是这样做的!感谢所有试图帮助的人!特别感谢@James !!
原帖
视频
The Video Showing All My Code, and the problem
描述
基本上,我正在尝试为我的项目创建文件夹(如果它们不存在),但是,当我运行创建文件夹的.py文件时,它只创建其中两个,然后声称所有文件已经存在。即使在文件浏览器中他们没有(当我做ls时它们也不存在于终端中)。我尝试将变量移动到同一个文件中,并且我试图摆脱if语句,我也尝试了os.mkdir()
&将existOk设置为true。我从来没有得到任何错误
控制台输出
首次运行(无文件夹存在)
True # Main Directory: It thinks the base directory already exists
False # Logs Directory: It knows that the other directories do not exist
False # Users Directory: ^
False # Inventory Directory: ^^
False # Stats Directory: ^^^
../FrozenDesert # It Created The Base Directory
../FrozenDesert/Users # It Created The Users Directory
第二次运行(用户和主要目录存在)
True # Main Directory: Knows it exists
True # Logs Directory: Thinks it exists
True # Users Directory: Knows it exists
True # Inventory Directory: Thinks it exists
True # Stats Directory: Thinks it exists
# No Directories were created
守则
登录目录(不相关)
#Imports
import time
import os
import settings
# Request User Input
print('Welcome To The Frozen Desert. Would You Like To Register Or Sign In?')
print(" ")
while True:
# Store User Input
identity = raw_input("Login [L] / Register [R]: ")
# Checks To See If The Answer Is Appropriate
if identity.lower() not in ('l', 'r'):
continue
print(" ")
break
#TODO: Make Te Login & Registration Functions Work
# Begins User Login
if identity.lower() == "l":
print("Opening Login Pane")
time.sleep(1)
# Clears Screen
i = 0
while i <= 50:
print(" ")
i += 1
#Begins User Registration
if identity.lower() == "r":
print("Opening Registration Pane")
# Clears Screen
i = 0
time.sleep(1)
while i <= 50:
print(" ")
i += 1
设置目录(变量)
# Imports
import os
# Define Locations
pathBase = '../FrozenDesert'
pathUsers = pathBase + '/Users'
pathInventory = pathUsers + '/Inventory'
pathStats = pathUsers + '/Stats'
pathLogs = pathBase + '/Logs'
directoryMain = os.path.dirname(pathBase)
directoryUsers = os.path.dirname(pathUsers)
directoryUserStats = os.path.dirname(pathStats)
directoryUserInventory = os.path.dirname(pathInventory)
directoryLogs = os.path.dirname(pathLogs)
启动管理(创建目录和资料)
# Imports
import os
import settings
# Create Directories
print(os.path.isdir(settings.directoryMain))
print(os.path.isdir(settings.directoryLogs))
print(os.path.isdir(settings.directoryUsers))
print(os.path.isdir(settings.directoryUserInventory))
print(os.path.isdir(settings.directoryUserStats))
# TODO: Make Evrything Below Work
# Main Directory
if not os.path.isdir(settings.directoryMain):
print(settings.directoryMain)
os.makedirs(settings.directoryMain)
# Users Directory
if not os.path.isdir(settings.directoryUsers):
print(settings.directoryUsers)
os.makedirs(settings.directoryUsers)
# Logs Directory
if not os.path.exists(settings.directoryLogs):
print(settings.directoryLogs)
os.makedirs(settings.directoryLogs)
# User-Stats Directory
if not os.path.isdir(settings.directoryUserStats):
print(settings.directoryUserStats)
os.makedirs(settings.directoryUserStats)
# User-Inventory Directory
if not os.path.isdir(settings.directoryUserInventory):
print(settings.directoryUserInventory)
os.makedirs(settings.directoryUserInventory)
答案 0 :(得分:2)
过去我运气好了
if not os.path.exists(dir):
os.mkdir(dir)
如果那不起作用那么它可能就是你正在使用的相对文件路径。您可以尝试使用类似
之类的内容在运行时获取基本路径的完整路径base_path = os.path.dirname(os.path.realpath(__file__))
虽然我对你的目录结构并不完全肯定,但在获取工作目录后可能需要进行调整。
答案 1 :(得分:1)
您可以使用os
检查路径是否存在,如果存在,请检查它是否是目录,否则创建新目录。
import os
def make_new_dir(d):
if os.path.exists(d):
if os.path.isdir(d):
return False
os.path.mkdir(d)
return True