<p>我认为以下代码适用于您:</p>
<pre><code>from pathlib import Path
job_list = ['project_name1', 'project_name2', 'project_name3']
def create_log_file(job_list):
log_file_info = []
for job in job_list:
file_name = os.path.join('\\network_location\\folder\\subfolder1\\subfolder2\\subfolder3\\Jenkins_Build\\buildlog\\windows\\', job + "_" + current_date + file_format)
file_content = get_console_output(job)
with open(file_name, 'w', encoding="utf-8") as f:
f.write(file_content)
log_file = ['{}: {}'.format(job, Path(file_name))]
log_file_info.append(log_file)
return log_file_info
print(create_log_file(job_list))
</code></pre>
<p>请注意开头的进口声明。当使用Path(文件名)时,您告诉解释器显式地将给定的字符串视为Path。
我还删除了<code>.format</code>语句,并用<strong>转义反斜杠更改了所有斜杠</p>
<blockquote>
<blockquote>
<p>For more information about python paths you can check the following articles:</p>
<p><strong><a href="https://docs.python.org/3/library/pathlib.html" rel="nofollow noreferrer">click</a></strong></p>
<p><strong><a href="https://docs.python.org/2/library/os.path.html" rel="nofollow noreferrer">click</a></strong></p>
</blockquote>
</blockquote>