如何打开签出文件P4 Python

2024-05-14 11:16:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用Python编写一个带有P4Python API的脚本,它自动化了在Perforce中签出文件并对其进行一些更改的过程。我目前正试图找出如何打开已签出的文件,以便对其进行更改,但无法使用“//depot”文件路径打开它。我假设我需要使用系统文件路径(C:/…),但我不确定如何继续

## 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()

Tags: 文件to路径txtapitreeserverfile1
2条回答

Python的open函数在本地文件上运行,并且没有仓库路径的概念,因此正如您所说的,您需要使用工作区路径。方便地说,这是作为p4 edit输出的一部分返回的,因此您可以从那里获取它:

from P4 import P4

p4 = P4()

## Connect to P4 server
p4.connect()

## Checkout file to default changelist
local_paths = [
    result['clientFile'] 
    for result in p4.run_edit("//depot/file/tree/fileToEdit.txt")
]

for path in local_paths:
    with open(path, 'w') as f:
        f.write("THIS IS A TEST")

## Disconnect from P4 server
p4.disconnect()

请注意,如果p4 edit命令未打开文件,例如,如果文件未同步(在这种情况下,您的脚本可能希望p4 sync),或者如果文件已打开(在这种情况下,您可能只想从p4 opened获取本地路径),此简单脚本将无法工作并且无论如何都要修改它,或者您可能希望首先还原现有的更改,或者可能什么都不做),或者如果文件不存在(在这种情况下,您可能希望p4 add

以下代码为我提供了本地文件路径:

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

使用p4.run_fstat(“//depot/filename”)将提供所有必要的信息,额外的“[0]['clientFile']”用于提取本地文件路径

相关问题 更多 >

    热门问题