Search code examples
pythonperforcep4python

How to open a checked out file P4 Python


I am currently writing a script in Python with the P4Python API which automates the process of checking out a file in Perforce and making some changes to it. I'm currently trying to figure out how to open the file which has been checked out so I can make changes to it but I cannot open it using the "//depot" file path. I'd assume I need to use the system file path (C:/...) but I'm not sure how to proceed.

## Connect to P4 server
p4.connect()

## Checkout file to default changelist
p4.run_edit("//depot/file/tree/fileToEdit.txt")

f1 = "//depot/file/tree/fileToEdit.txt" ## This file path does not work

with open(f1, 'w') as file1:
    file1.write("THIS IS A TEST")

## Disconnect from P4 server
p4.disconnect()

Solution

  • The following bit of code provided me with the local file path:

    result = p4.run_fstat("//depot/file/tree/fileToEdit.txt")[0]['clientFile']
    print(result)
    

    Using p4.run_fstat("//depot/filename") will provide all the information necessary, the additional "[0]['clientFile']" was to pull the local file path.