I am attempting to write a quick piece of code that will check to see if a directory exists, if it does it will print that it does, if it does request permission to overwrite, and if not prompt to make the directory.
Here is what I have so far:
def mk_prjDir():
print "Checking to see if", prjDir, "exists.."
dirList = os.listdir(prjDir)
for d in dirList:
if os.path.isdir(d):
print (prjDir, "exists!")
else:
print (prjDir, "does not exist!")
But every time I run it, if the directory isn't there it just dumps with an error. What am I doing wrong?
Update with traceback:
Checking to see if /directory exists..
Traceback (most recent call last):
File "alpha.py", line 73, in <module>
mk_prjDir()
File "alpha.py", line 50, in mk_prjDir
dirList = os.listdir(prjDir)
OSError: [Errno 2] No such file or directory: '/directory'
Update #2: ospath was a typo, but the error remains.
I want to try and catch that exception. Should I be using a while == True: and a try/except? If so, what does that look like?