File system operations

The RPA.FileSystem library allows creating, copying, moving, overwriting, deleting or removing, checking for existence, listing files and directories, and many more file system operations. Here are some examples (check the RPA.FileSystem library documentation for all the available keywords):

Robot Framework example

*** Settings ***
Documentation     Examples of file operations.
Library           RPA.FileSystem

*** Variables ***
${NEW_DIR_1}=     new-dir-1
${NEW_DIR_2}=     new-dir-2
${NEW_DIR_2_COPY}=    copy-of-new-dir-2
${TEXT_FILE}=     file.txt
${TEXT_FILE_COPY}=    copy-of-file.txt

*** Tasks ***
File operations
    ${dir_exists}=    Does Directory Exist    ${CURDIR}
    ${dir_does_not_exist}=    Does Directory Not Exist    ${CURDIR}
    ${dir_tree}=    Log Directory Tree    ${CURDIR}
    ${dir_is_empty}=    Is Directory Empty    ${CURDIR}
    ${dir_is_not_empty}=    Is Directory Not Empty    ${CURDIR}
    Create File    ${TEXT_FILE}    Hello    overwrite=True
    Wait Until Created    ${TEXT_FILE}
    Touch File    ${TEXT_FILE}
    ${file_exists}=    Does File Exist    ${TEXT_FILE}
    ${file_does_not_exist}=    Does File Not Exist    ${TEXT_FILE}
    ${file_is_empty}=    Is File Empty    ${TEXT_FILE}
    ${file_is_not_empty}=    Is File Not Empty    ${TEXT_FILE}
    ${found_files}=    Find Files    \*.txt
    Append To File    ${TEXT_FILE}    World!
    ${text_content}=    Read File    ${TEXT_FILE}
    ${absolute_path}=    Absolute Path    ${TEXT_FILE}
    ${created}=    Get File Creation Date    ${TEXT_FILE}
    ${extension}=    Get File Extension    ${TEXT_FILE}
    ${modified}=    Get File Modified Date    ${TEXT_FILE}
    ${name}=    Get File Name    ${TEXT_FILE}
    ${size}=    Get File Size    ${TEXT_FILE}
    Copy File    ${TEXT_FILE}    ${TEXT_FILE_COPY}
    ${files}=    List Files In Directory    ${CURDIR}
    ${directories}=    List Directories In Directory    ${CURDIR}
    Create Directory    ${NEW_DIR_1}
    ${files_to_move}=    Create List    ${TEXT_FILE}    ${TEXT_FILE_COPY}
    Move Files    ${files_to_move}    ${NEW_DIR_1}    overwrite=True
    Run Keyword If File Exists
    ...    ${NEW_DIR_2}/${TEXT_FILE}
    ...    Remove Directory    ${NEW_DIR_2}    recursive=True
    Move Directory    ${NEW_DIR_1}    ${NEW_DIR_2}    overwrite=True
    Run Keyword And Ignore Error
    ...    Copy Directory    ${NEW_DIR_2}    ${NEW_DIR_2_COPY}
    Run Keyword And Ignore Error
    ...    Change File Extension
    ...    ${NEW_DIR_2}/${TEXT_FILE_COPY}
    ...    .md
    Empty Directory    ${NEW_DIR_2_COPY}

Python example

from os import getcwd
from time import sleep
from RPA.FileSystem import FileSystem

file_system = FileSystem()
new_dir_1 = "new-dir-1"
new_dir_2 = "new-dir-2"
new_dir_2_copy = "copy-of-new-dir-2"
text_file = "file.txt"
text_file_copy = "copy-of-file.txt"


def file_operations():
    dir_exists = file_system.does_directory_exist(getcwd())
    dir_does_not_exist = file_system.does_directory_not_exist(getcwd())
    dir_tree = file_system.log_directory_tree(getcwd())
    dir_is_empty = file_system.is_directory_empty(getcwd())
    dir_is_not_empty = file_system.is_directory_empty(getcwd())
    file_system.create_file(text_file, "Hello", overwrite=True)
    file_system.wait_until_created(text_file)
    file_system.touch_file(text_file)
    file_exists = file_system.does_file_exist(text_file)
    file_does_not_exist = file_system.does_file_not_exist(text_file)
    file_is_empty = file_system.is_file_empty(text_file)
    file_is_not_empty = file_system.is_file_not_empty(text_file)
    found_files = file_system.find_files("\* .txt")
    file_system.append_to_file(text_file, "World!")
    text_content = file_system.read_file(text_file)
    absolute_path = file_system.absolute_path(text_file)
    created = file_system.get_file_creation_date(text_file)
    extension = file_system.get_file_extension(text_file)
    modified = file_system.get_file_modified_date(text_file)
    name = file_system.get_file_name(text_file)
    size = file_system.get_file_size(text_file)
    file_system.copy_file(text_file, text_file_copy)
    files = file_system.list_files_in_directory(getcwd())
    directories = file_system.list_directories_in_directory(getcwd())
    file_system.create_directory(new_dir_1)
    files_to_move = [text_file, text_file_copy]
    file_system.move_files(files_to_move, new_dir_1, overwrite=True)

    if file_system.does_file_exist(f"{new_dir_2}/{text_file}"):
        file_system.remove_directory(new_dir_2, recursive=True)

    file_system.move_directory(new_dir_1, new_dir_2, overwrite=True)

    try:
        file_system.copy_directory(new_dir_2, new_dir_2_copy)
        file_system.change_file_extension(
            "{new_dir_2}/{text_file_copy}", ".md")
    except:
        print("Ignored error.")

    file_system.empty_directory(new_dir_2_copy)


def main():
    file_operations()


if __name__ == "__main__":
    main()

Learn more about the libraries mentioned on this page: