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/foo
gives us a list containing one file (becausefoo
is open for edit), andp4 opened //stream/test/bar
gives us an empty list (becausebar
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, includingif
statements and thenot
operator: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
orFalse
value (e.g. to return from a function that is declared as returning an exact boolean value), you can use thebool
function to convert a truthy value intoTrue
and a falsey value intoFalse
:or use a
len()
comparison, which amounts to the same thing: