michelle
michelle

Reputation: 13

Python equivalent of the ! operator

For the following construct what can the more pythonic way?

If it were C I could just use ! but what is it equivalent in Python?

file_path = # something 
if os.path.exists(file_path):
     pass
else:
  file_path = # make new path 

Upvotes: 1

Views: 203

Answers (3)

Keith
Keith

Reputation: 43044

The not keyword.

if not os.path.exists(file_path):
    file_path ...

Upvotes: 1

Kos
Kos

Reputation: 72299

Simply:

file_path = # something 
if not os.path.exists(file_path):
    file_path = # make new path 

Operator not is also in C++, btw, as a synonym for !.

Upvotes: 0

Jacob
Jacob

Reputation: 43269

file_path = # something 
if not os.path.exists(file_path):
     file_path = #make new path

Python Docs - Expressions - 5.1 Boolean Expressions

Upvotes: 4

Related Questions