p4.run("opened") returns a list of results (dicts) corresponding to open files, which will be empty if no files are opened within the path specification you provided. Try just printing out the value, or better yet running it in the REPL, to get a better understanding of what the function returns:
We can see that running p4 opened //stream/test/foo gives us a list containing one file (because foo is open for edit), and p4 opened //stream/test/bar gives us an empty list (because bar is not open for anything).
In Python a list is "falsey" if empty and "truthy" if non-empty. This is not the same as being == False and == True, but it does work in most other contexts where a boolean is expected, including if statements and the not operator:
>>> if p4.run("opened", "//stream/test/foo"):
... print("foo is open")
...
foo is open
>>> if not p4.run("opened", "//stream/test/bar"):
... print("bar is not open")
...
bar is not open
It's considered perfectly Pythonic to use lists in this way (it's why the concept of "truthiness" exists in the language) instead of using explicit True/False values.
If you do need an exact True or False value (e.g. to return from a function that is declared as returning an exact boolean value), you can use the bool function to convert a truthy value into True and a falsey value into False:
p4.run("opened")returns a list of results (dicts) corresponding to open files, which will be empty if no files are opened within the path specification you provided. Try just printing out the value, or better yet running it in the REPL, to get a better understanding of what the function returns:We can see that running
p4 opened //stream/test/foogives us a list containing one file (becausefoois open for edit), andp4 opened //stream/test/bargives us an empty list (becausebaris not open for anything).In Python a list is "falsey" if empty and "truthy" if non-empty. This is not the same as being
== Falseand== True, but it does work in most other contexts where a boolean is expected, includingifstatements and thenotoperator:It's considered perfectly Pythonic to use lists in this way (it's why the concept of "truthiness" exists in the language) instead of using explicit
True/Falsevalues.If you do need an exact
TrueorFalsevalue (e.g. to return from a function that is declared as returning an exact boolean value), you can use theboolfunction to convert a truthy value intoTrueand a falsey value intoFalse:or use a
len()comparison, which amounts to the same thing: