Python API

FileSystem

Files and filesystems library for Robot Framework

class RPA.FileSystem.Directory

Bases: tuple

Robot Framework -friendly container for directories.

count()

Return number of occurrences of value.

classmethod from_path(path)

Create a directory object from pathlib.Path or a path string.

index()

Return first index of value.

Raises ValueError if the value is not present.

property name

Alias for field number 1

property path

Alias for field number 0

class RPA.FileSystem.File

Bases: tuple

Robot Framework -friendly container for files.

count()

Return number of occurrences of value.

classmethod from_path(path)

Create a File object from pathlib.Path or a path string.

index()

Return first index of value.

Raises ValueError if the value is not present.

property mtime

Alias for field number 3

property name

Alias for field number 1

property path

Alias for field number 0

property size

Alias for field number 2

class RPA.FileSystem.FileSystem

Bases: object

The FileSystem library can be used to interact with files and directories on the local computer. It can inspect and list files, remove and create them, read contents from files, and write data out.

It shadows the built-in OperatingSystem library but contains keywords which are more RPA-oriented.

Examples

Robot Framework

The library allows, for instance, iterating over files and inspecting them.

*** Settings ***
Library    RPA.FileSystem

*** Keywords ***
Delete large files
    ${files}=    List files in directory    archive/orders/
    FOR    ${file}  IN  @{FILES}
        Run keyword if    ${file.size} > 10**8    Remove file    ${file}
    END

Read process output
    Start external program
    Wait until modified    process.log
    ${output}=  Read file  process.log
    [Return]    ${output}

Python

The library can also be used inside Python.

from RPA.FileSystem import FileSystem

def move_to_archive():
    lib = FileSystem()

    matches = lib.find_files("**/*.xlsx")
    if matches:
        lib.create_directory("archive")
        lib.move_files(matches, "archive")
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
absolute_path(path) → str

Returns the absolute path to a file, and resolves symlinks.

Parameters

path – path that will be resolved

Returns

absolute path to file as a string

append_to_binary_file(path, content) → None

Appends binary content to the given file.

Parameters
  • path – path to file to append to

  • content – content to append

append_to_file(path, content, encoding='utf-8') → None

Appends text to the given file.

Parameters
  • path – path to file to append to

  • content – content to append

  • encoding – character encoding of appended content

change_file_extension(path, extension) → None

Replaces file extension for file at given path.

Parameters
  • path – path to file to rename

  • extension – new extension, e.g. .xlsx

copy_directory(source, destination) → None

Copy directory from source path to destination path.

Parameters
  • source – path to source directory

  • destination – path to copy destination

copy_file(source, destination) → None

Copy a file from source path to destination path.

Parameters
  • source – path to source file

  • destination – path to copy destination

copy_files(sources, destination) → None

Copy multiple files to destination folder.

Parameters
  • sources – list of source files

  • destination – path to destination folder

create_binary_file(path, content=None, overwrite=False) → None

Creates a new binary file, and writes content if any is given.

Parameters
  • path – path to file to write

  • content – content to write to file (optional)

  • overwrite – replace destination file if it already exists

create_directory(path, parents=False, exist_ok=True) → None

Creates a directory and (optionally) non-existing parent directories.

Parameters
  • path – path to new directory

  • parents – create missing parent directories

  • exist_ok – continue without errors if directory already exists

create_file(path, content=None, encoding='utf-8', overwrite=False) → None

Creates a new text file, and writes content if any is given.

Parameters
  • path – path to file to write

  • content – content to write to file (optional)

  • encoding – character encoding of written content

  • overwrite – replace destination file if it already exists

does_directory_exist(path) → bool

Returns True if the given directory exists, False if not.

Parameters

path – path to inspected directory

Returns

true or false if the directory exists

does_directory_not_exist(path) → bool

Returns True if the directory does not exist, False if it does.

Parameters

path – path to inspected directory

Returns

true or false if the directory does not exists

does_file_exist(path) → bool

Returns True if the given file exists, False if not.

Parameters

path – path to inspected file

Returns

true or false if file exists

does_file_not_exist(path) → bool

Returns True if the file does not exist, False if it does.

Parameters

path – path to inspected file

Returns

true or false if the files does not exist

empty_directory(path) → None

Removes all the files in the given directory.

Parameters

path – directory to remove files from

find_files(pattern, include_dirs=True, include_files=True) → list

Find files recursively according to a pattern.

Parameters
  • pattern – search path in glob format pattern, e.g. .xls or */orders.txt

  • include_dirs – include directories in results

  • include_files – include files in results

Returns

list of paths that match the pattern

get_file_creation_date(path) → float

Returns the creation time in seconds. Note: Linux sets this whenever file metadata changes

Parameters

path – path to file to inspect

Returns

creation time in seconds as a float

get_file_extension(path) → str

Returns the suffix for the file.

Parameters

path – path to file

Returns

file suffix as a string

get_file_modified_date(path) → float

Returns the modified time in seconds.

Parameters

path – path to file to inspect

Returns

modified time in seconds as a float

get_file_name(path) → str

Returns only the filename portion of a path.

Parameters

path – path to file

Returns

filename portion of a path as a string

get_file_owner(path) → str

Return the name of the user who owns the file.

Parameters

path – path to file to inspect

Returns

file owner as a string

get_file_size(path) → int

Returns the file size in bytes.

Parameters

path – path to file to inspect

Returns

file size in bytes as an int

is_directory_empty(path=None) → bool

Returns True if the given directory has no files or subdirectories.

Parameters

path – path to inspected directory

Returns

true or false if the directory is empty

is_directory_not_empty(path=None) → bool

Returns True if the given directory has any files or subdirectories.

Parameters

path – path to inspected directory

Returns

true or false if the directory is not empty

is_file_empty(path) → bool

Returns True if the given file has no content, i.e. has zero size.

Parameters

path – path to inspected file

Returns

true or false if the file is empty

is_file_not_empty(path) → bool

Returns True if the given file has content, i.e. larger than zero size.

Parameters

path – path to inspected file

Returns

true or false if the file is not empty

join_path(*parts) → str

Joins multiple parts of a path together.

Parameters

parts – Components of the path, e.g. dir, subdir, filename.ext

Returns

complete file path as a single string

list_directories_in_directory(path=None) → list

Lists all the directories in the given directory, relative to it.

Parameters

path – base directory for search, defaults to current working dir

Returns

list of directories in slected directory

list_files_in_directory(path=None) → list

Lists all the files in the given directory, relative to it.

Parameters

path – base directory for search, defaults to current working dir

Returns

list of files in directory

log_directory_tree(path=None) → None

Logs all the files in the directory recursively.

Parameters

path – base directory to start from, defaults to current working dir

move_directory(source, destination, overwrite=False) → None

Move a directory from source path to destination path.

Parameters
  • source – source directory path for moving

  • destination – path to move to

  • overwrite – replace destination directory if it already exists

move_file(source, destination, overwrite=False) → None

Move a file from source path to destination path, optionally overwriting the destination.

Parameters
  • source – source file path for moving

  • destination – path to move to

  • overwrite – replace destination file if it already exists

move_files(sources, destination, overwrite=False) → None

Move multiple files to the destination folder.

Parameters
  • sources – list of files to move

  • destination – path to move destination

  • overwrite – replace destination files if they already exist

normalize_path(path) → str

Removes redundant separators or up-level references from path.

Parameters

path – path that will be normalized

Returns

path to file as a string

read_binary_file(path) → bytes

Reads a file in binary mode and returns the content. Does not attempt to decode the content in any way.

Parameters

path – path to file to read

Returns

the file content as bytes

read_file(path, encoding='utf-8') → str

Reads a file as text, with given encoding, and returns the content.”

Parameters
  • path – path to file to read

  • encoding – character encoding of file

Returns

file content as string

remove_directory(path, recursive=False) → None

Removes the given directory, and optionally everything it contains.

Parameters
  • path – path to directory

  • recursive – remove all subdirectories and files

remove_file(path, missing_ok=True) → None

Removes the given file.

Parameters
  • path – path to the file to remove

  • missing_ok – ignore non-existent file

remove_files(*paths, missing_ok=True) → None

Removes multiple files.

Parameters
  • paths – paths to files to be removed

  • missing_ok – ignore non-existent files

run_keyword_if_file_exists(path, keyword, *args) → None

If file exists at path, execute given keyword with arguments.

Parameters
  • path – path to file to inspect

  • keyword – Robot Framework keyword to execute

  • args – arguments to keyword

Example:

Run keyword if file exists    orders.xlsx    Process orders
touch_file(path) → None

Creates a file with no content, or if file already exists, updates the modification and access times.

Parameters

path – path to file which is touched

wait_until_created(path, timeout=5.0) → str

Poll path until it exists, or raise exception if timeout is reached.

Parameters
  • path – path to poll

  • timeout – time in seconds until keyword fails

Returns

path to the created file as a string

wait_until_modified(path, timeout=5.0) → str

Poll path until it has been modified after the keyword was called, or raise exception if timeout is reached.

Parameters
  • path – path to poll

  • timeout – time in seconds until keyword fails

Returns

path to the modified file as a string

wait_until_removed(path, timeout=5.0) → None

Poll path until it doesn’t exist, or raise exception if timeout is reached.

Parameters
  • path – path to poll

  • timeout – time in seconds until keyword fails

exception RPA.FileSystem.TimeoutException

Bases: Exception

Exception raised from wait-prefixed keywords

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.