0

I want to search a Perforce depot for files. I do this from a python script and use the p4python library command:

list = p4.run("files", "//mypath/myfolder/*")

This works fine as long as myfolder contains some files. I get a python list as a return value. But when there is no file in myfolder the program stops running and no error message is displayed. My goal is to get an empty python list, so that I can see that this folder doesn't contain any files.

Does anybody has some ideas? I could not find information in the p4 files documentation and on StackOverflow.

colidyre
  • 4,170
  • 12
  • 37
  • 53
  • What do you mean by "crashes"? Is an exception thrown? – Andrew_Lvov Sep 14 '18 at 09:21
  • @Andrew_Lvov no exception is thrown! – Jakob Troidl Sep 14 '18 at 09:28
  • 1
    You need to get more information about the behavior, beyond "the program stops running and no error message is displayed". First, try the command at the command line, so you can see what the underlying Perforce behavior is. Assuming you are getting an error of some sort, figure out what your Python program is doing with error output. – Bryan Pendleton Sep 14 '18 at 13:38

2 Answers2

0

Try something like this ?

import os

if len(os.listdir('//mypath/myfolder/') ) == 0: # Do not execute p4.run if directory is empty
    list = []
else:    
    list = p4.run("files", "//mypath/myfolder/*")
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
  • This may be not work because the folder structure is located in the perforce depot and not locally safed in my harddrive! but thank you for your help :) – Jakob Troidl Sep 14 '18 at 09:33
0

I'm going to guess you've got an exception handler around that command execution that's eating the exception and exiting. I wrote a very simple test script and got this:

C:\Perforce\test>C:\users\samwise\AppData\local\programs\python\Python36-32\python files.py
Traceback (most recent call last):
  File "files.py", line 6, in <module>
    print(p4.run("files", "//depot/no such path/*"))
  File "C:\users\samwise\AppData\local\programs\python\Python36-32\lib\site-packages\P4.py", line 611, in run
    raise e
  File "C:\users\samwise\AppData\local\programs\python\Python36-32\lib\site-packages\P4.py", line 605, in run
    result = P4API.P4Adapter.run(self, *flatArgs)
P4.P4Exception: [P4#run] Errors during command execution( "p4 files //depot/no such path/*" )

        [Error]: "//depot/no such path/* - must refer to client 'Samwise-dvcs-1509687817'."
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Thank you very much! i removed the exception handler and i got a warning (not an exception). I could catch it with p4.warnings and handle it afterwards :) – Jakob Troidl Sep 17 '18 at 07:36