negation

Negation in Python

丶灬走出姿态 提交于 2019-11-26 12:56:20
问题 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(