Robot Framework

File operations

This is what you can do with a file in Robot Framework:

*** Settings ***
Library    OperatingSystem


*** Variables ***
${MY_NEW_DIR}    ${CURDIR}${/}my_new_directory    # CURDIR = built-in variable for current directory path
${MY_FILE}       ${MY_NEW_DIR}${/}my_file.txt
${MY_COPY}       ${MY_NEW_DIR}${/}my_copy.txt


*** Tasks ***
My Task
    Create File    ${MY_FILE}    Well hello there!
    File Should Exist    ${MY_FILE}
    File Should Not Be Empty    ${MY_FILE}
    ${my_content}=    Get File    ${MY_FILE}
    Log To Console    my_content: ${my_content}    # Well Hello there!
    Append To File    ${MY_FILE}    Another hello!
    ${lines_containing_hello}=    Grep File    ${MY_FILE}    hello
    Log To Console    lines_containing_hello: ${lines_containing_hello}    # Well hello there!Another hello!
    Copy File    ${MY_FILE}    ${MY_COPY}
    File Should Exist    ${MY_COPY}
    ${my_new_dir_file_count}=    Count Files In Directory    ${MY_NEW_DIR}
    Log To Console    my_new_dir_file_count: ${my_new_dir_file_count}    # 2
    Move File    ${MY_COPY}    ${TEMPDIR}    # TEMPDIR = OS dependent built-in variable
    File Should Exist    ${TEMPDIR}${/}my_copy.txt
    File Should Not Exist    ${MY_COPY}
    ${my_file_size_in_bytes}=    Get File Size    ${MY_FILE}
    Log To Console    my_file_size: ${my_file_size_in_bytes}    # 31
    Remove File    ${MY_FILE}
    File Should Not Exist    ${MY_FILE}
    Touch    ${MY_FILE}
    File Should Exist    ${MY_FILE}
    File Should Be Empty    ${MY_FILE}

Related