简体   繁体   中英

Working with special character backslash in Python 3.x

I'm currently working on creating log file for Jenkins build using Python script and Jenkins API.

The following function create text file of build's console output and save it into network location. I'm looking into getting file network path so that it can be included in the customised email notifications.

Just wondering if you could help me to get expected result. Thank you.

Code:

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, file_name.replace("/", "\\"))]            
        log_file_info.append(log_file)
    return log_file_info     

Expected Result:

[['project_name1: \\network_location\\folder\\subfolder1\\subfolder2\\subfolder3\\Jenkins_Build\\buildlog\\windows\\project_name1_2018-10-24.txt'], ['project_name2: \\network_location\\folder\\subfolder1\\subfolder2\\subfolder3\\Jenkins_Build\\buildlog\\windows\\project_name2_2018-10-24.txt'], ['project_name3: \\network_location\\folder\\subfolder1\\subfolder2\\subfolder3\\Jenkins_Build\\buildlog\\windows\\project_name3_2018-10-24.txt']]

Actual Result:

[['project_name1: \\\\\\\\network_location\\\\folder\\\\subfolder1\\\\subfolder2\\\\subfolder3\\\\Jenkins_Build\\\\buildlog\\\\windows\\\\project_name1_2018-10-24.txt'], ['project_name2: \\\\\\\\network_location\\\\folder\\\\subfolder1\\\\subfolder2\\\\subfolder3\\\\Jenkins_Build\\\\buildlog\\\\windows\\\\project_name2_2018-10-24.txt'],
['project_name3: \\\\\\\\network_location\\\\folder\\\\subfolder1\\\\subfolder2\\\\subfolder3\\\\Jenkins_Build\\\\buildlog\\\\windows\\\\project_name3_2018-10-24.txt']]

Your actual result is OK the way it is. Python escapes backslash characters with two backslashs (\\\\). Try to display your results with the print function and you will see that it actually matches what you want.

>>> print[0]["project_name1"]

I think that the following code would work for you:

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

Please note the import statement in the beginning. When using Path(file_name), you are telling the interpreter to explicitly treat the given string as path. Also I have removed .format statement and have changed all slashes with escaped backslashes .

For more information about python paths you can check the following articles:

click

click

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM