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()
try instead:
if not os.path.exists(pathName):
do this
Combining the input from everyone else (use not, no parens, use os.mkdir
) you'd get...
special_path_for_john = "/usr/share/sounds/blues"
if not os.path.exists(special_path_for_john):
os.mkdir(special_path_for_john)
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.
Source: Stackoverflow.com