I’m trying to create a directory if the path doesn’t exist, but the ! (not) operator doesn’t work. I’m not sure how to negate in Python… What’s the correct way to do this?
if (!os.path.exists("/usr/share/sounds/blues")):
proc = subprocess.Popen(["mkdir", "/usr/share/sounds/blues"])
proc.wait()
The negation operator in Python is
not. Therefore just replace your!withnot.For your example, do this:
For your specific example (as Neil said in the comments), you don’t have to use the
subprocessmodule, you can simply useos.mkdir()to get the result you need, with added exception handling goodness.Example: