I need to check file is opened in perforce or not like this:
if((p4.run("opened", self.file) != True):
but it's not the right way I think it's always true can you please help to solve this Thanks
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:
>>> from P4 import P4
>>> p4 = P4()
>>> p4.connect()
P4 [Samwise@Samwise-dvcs-1509687817 rsh:p4d.exe -i -r "c:\Perforce\test\.p4root"] connected
>>> p4.run("opened", "//...")
[{'depotFile': '//stream/test/foo', 'clientFile': '//Samwise-dvcs-1509687817/foo', 'rev': '2', 'haveRev': '2', 'action': 'edit', 'change': 'default', 'type': 'text', 'user': 'Samwise', 'client': 'Samwise-dvcs-1509687817'}]
>>> p4.run("opened", "//stream/test/foo")
[{'depotFile': '//stream/test/foo', 'clientFile': '//Samwise-dvcs-1509687817/foo', 'rev': '2', 'haveRev': '2', 'action': 'edit', 'change': 'default', 'type': 'text', 'user': 'Samwise', 'client': 'Samwise-dvcs-1509687817'}]
>>> p4.run("opened", "//stream/test/bar")
[]
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
:
>>> bool(p4.run("opened", "//stream/test/foo"))
True
>>> bool(p4.run("opened", "//stream/test/bar"))
False
or use a len()
comparison, which amounts to the same thing:
>>> len(p4.run("opened", "//stream/test/foo")) > 0
True
>>> len(p4.run("opened", "//stream/test/bar")) > 0
False