Negation in Python
问题 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() 回答1: 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(