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
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
Copyright © 2021 Jogjafile Inc.
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: