It's only a syntax error if you are using Python 2 and haven't used
from __future__ import print_function
because you can't use a print
statement as part of the conditional expression.
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "foo" if False else print("error")
File "<stdin>", line 1
"foo" if False else print("error")
^
SyntaxError: invalid syntax
>>> from __future__ import print_function
>>> "foo" if False else print("error")
error
However, your code is susceptible to a race condition. If some other process creates the directory after you check for it but before you try to create it, your code with raise an error. Just try to create the directory, and catch any exception that occurs as a result.
# Python 2
import errno
try:
os.makedirs(nombre)
except OSError as exc:
if exc.errno != errno.EEXISTS:
raise
print ("Directorio existente")
# Python 3
try:
os.makedirs(nombre)
except FileExistsError:
print ("Directorio existente")