Negation in Python

Negation in Python

The negation operator in Python is not. Therefore just replace your ! with not.

For your example, do this:

if not os.path.exists(/usr/share/sounds/blues) :
    proc = subprocess.Popen([mkdir, /usr/share/sounds/blues])

For your specific example (as Neil said in the comments), you dont have to use the subprocess module, you can simply use os.mkdir() to get the result you need, with added exception handling goodness.


blues_sounds_path = /usr/share/sounds/blues
if not os.path.exists(blues_sounds_path):
    except OSError:
        # Handle the case where the directory could not be created.

Python prefers English keywords to punctuation. Use not x, i.e. not os.path.exists(...). The same thing goes for && and || which are and and or in Python.

Negation in Python

try instead:

if not os.path.exists(pathName):
    do this

Leave a Reply

Your email address will not be published.