0

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?

3 Answers3

0

os.listdir(dir) returns you a list of files found in the directory dir. As the error says, this directory does not exists, so, obviously, listdir can't list its contents.

Why are you doing this loop? Just use os.path.isdir to test if your directory exists.

kirelagin
  • 13,248
  • 2
  • 42
  • 57
0

Also see What is the naming convention in Python for variable and function names?

def mk_prjDir():
    print "Checking to see if", prjDir, "exists.."
    if os.path.isdir(prjDir):
        print (prjDir, "exists!")
    else:
        print (prjDir, "does not exist!")
Community
  • 1
  • 1
Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44
0

Firstly prjDir must be a full path to prjDir, i.e "\home\my_direc\", othwerise os.listdir will try to find prjDir in the current working directory of the script.

Secondly as os.listdir returns only file/directory names not the full path, so to get the full path you can use os.path.join(prjDir,d).

import os
def mk_prjDir():
    print "Checking to see if", prjDir, "exists.."
    dirList = os.listdir(prjDir)
    for d in dirList:
        if os.path.isdir(os.path.join(prjDir,d)):
            print (prjDir, "exists!")
        else:
            print (prjDir, "does not exist!")
mk_prjDir()

Update: I think I misunderstood your question if you only want to check whether a directory exists or not then try this:

def mk_prjDir():
    print "Checking to see if", prjDir, "exists.."
    if os.path.isdir(prjDir):
        print (prjDir, "exists!")
    else:
        print (prjDir, "does not exist!")
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • The only thing I am noticing is that it seems that the text isn't being outputted correctly, I get `('/directory', 'does not exist!')` as output instead of just `/directory does not exist!` Why would that be? –  May 30 '13 at 17:35
  • And I've tried with and without quotes, with and without brackets, etc. –  May 30 '13 at 17:35
  • 1
    os.path.isdir() duplicates os.path.exists() exist functionality, exists() unnecessary. – Ruslan Osipov May 30 '13 at 17:37
  • 1
    use just `print prjDir,"exists!"`, print is a statement in py2.x not function. – Ashwini Chaudhary May 30 '13 at 17:37
  • @rosipov os.path.exists doesn't differentiate between filenames or directory names, so it's checking first to see if it's even there and then if it's a directory (which is useful because it may catch an error in the filesystem) –  May 30 '13 at 17:38
  • @AshwiniChaudhary You are my hero :) –  May 30 '13 at 17:40
  • @JacobColeman I think rosipov is correct, `os.path.isdir()` is enough in this case. – Ashwini Chaudhary May 30 '13 at 17:43