RPA.FileSystem

module RPA.FileSystem

class RPA.FileSystem.FileSystem

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.

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

PATH_TYPE

alias of Union[str, Path]


variable ROBOT_LIBRARY_DOC_FORMAT

ROBOT_LIBRARY_DOC_FORMAT = 'REST'

variable ROBOT_LIBRARY_SCOPE

ROBOT_LIBRARY_SCOPE = 'GLOBAL'

method absolute_path

absolute_path(path: Union[str, Path])

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

method append_to_binary_file

append_to_binary_file(path: Union[str, Path], content: Any)

Appends binary content to the given file.

See Create Binary File for usage example.

Parameters
  • path – path to file to append to
  • content – content to append

method append_to_file

append_to_file(path: Union[str, Path], content: str, encoding: str = 'utf-8')

Appends text to the given file.

See Create File for usage example.

Parameters
  • path – path to file to append to
  • content – content to append
  • encoding – character encoding of appended content

method change_file_extension

change_file_extension(path: Union[str, Path], extension: str)

Replaces file extension for file at given path. the file extension can be removed by passing an empty string.

Parameters
  • path – path to file to rename
  • extension – new extension, e.g. .xlsx


method copy_directory

copy_directory(source: Union[str, Path], destination: Union[str, Path])

Copy directory from source path to destination path.

Parameters
  • source – path to source directory
  • destination – path to copy destination


method copy_file

copy_file(source: Union[str, Path], destination: Union[str, Path])

Copy a file from source path to destination path.

See Is Directory Empty for usage example.

Parameters
  • source – path to source file
  • destination – path to copy destination

method copy_files

copy_files(sources: List[Union[str, Path]], destination: Union[str, Path])

Copy multiple files to destination folder.

Parameters
  • sources – list of source files
  • destination – path to destination folder


method create_binary_file

create_binary_file(path: Union[str, Path], content: Optional[Any] = None, overwrite: bool = False)

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


method create_directory

create_directory(path: Union[str, Path], parents: bool = False, exist_ok: bool = True)

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

Parameters
  • path – path to new directory
  • parents – create missing parent directories (defaults to False)
  • exist_ok – continue without errors if directory already exists (defaults to True)


method create_file

create_file(path: Union[str, Path], content: Optional[str] = None, encoding: str = 'utf-8', overwrite: bool = False)

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 (default utf-8)
  • overwrite – replace destination file if it already exists (default False)


method does_directory_exist

does_directory_exist(path: Union[str, Path])

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

See Does Directory Not Exist for usage example.

  • Parameters: path – path to inspected directory
  • Returns: true or false if the directory exists

method does_directory_not_exist

does_directory_not_exist(path: Union[str, Path])

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


method does_file_exist

does_file_exist(path: Union[str, Path])

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

  • Parameters: path – path to inspected file
  • Returns: true or false if file exists


method does_file_not_exist

does_file_not_exist(path: Union[str, Path])

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

See Does File Exist for usage example.

  • Parameters: path – path to inspected file
  • Returns: true or false if the files does not exist

method empty_directory

empty_directory(path: Union[str, Path])

Removes all the files in the given directory.

  • Parameters: path – directory to remove files from


method find_files

find_files(pattern: Union[str, Path], include_dirs: bool = True, include_files: bool = True)

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 (defaults to True)
  • include_files – include files in results (defaults to True)
  • Returns: list of paths that match the pattern


method get_file_creation_date

get_file_creation_date(path: Union[str, Path])

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

method get_file_extension

get_file_extension(path: Union[str, Path])

Returns the suffix for the file.

  • Parameters: path – path to file
  • Returns: file suffix as a string

method get_file_modified_date

get_file_modified_date(path: Union[str, Path])

Returns the modified time in seconds.

  • Parameters: path – path to file to inspect
  • Returns: modified time in seconds as a float

method get_file_name

get_file_name(path: Union[str, Path])

Returns only the full file name portion of a path.

  • Parameters: path – path to file
  • Returns: filename portion of a path as a string

method get_file_owner

get_file_owner(path: Union[str, Path])

Return the name of the user who owns the file.

  • Parameters: path – path to file to inspect
  • Returns: file owner as a string

method get_file_size

get_file_size(path: Union[str, Path])

Returns the file size in bytes.

  • Parameters: path – path to file to inspect
  • Returns: file size in bytes as an int

method get_file_stem

get_file_stem(path: Union[str, Path])

Returns the name of the file without its extension.

  • Parameters: path – path to file
  • Returns: filename without its suffix as a string

method is_directory_empty

is_directory_empty(path: Optional[Union[str, Path]] = None)

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


method is_directory_not_empty

is_directory_not_empty(path: Optional[Union[str, Path]] = None)

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

See Is Directory Empty for usage example.

  • Parameters: path – path to inspected directory
  • Returns: true or false if the directory is not empty

method is_file_empty

is_file_empty(path: Union[str, Path])

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

method is_file_not_empty

is_file_not_empty(path: Union[str, Path])

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


method join_path

join_path(*parts: Union[str, Path])

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


method list_directories_in_directory

list_directories_in_directory(path: Optional[Union[str, Path]] = None)

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 selected directory


method list_files_in_directory

list_files_in_directory(path: Optional[Union[str, Path]] = None)

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

  • Parameters: path – base directory for search, defaults to current working directory
  • Returns: list of files in directory


method log_directory_tree

log_directory_tree(path: Optional[Union[str, Path]] = None)

Logs all the files in the directory recursively.

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


method move_directory

move_directory(source: Union[str, Path], destination: Union[str, Path], overwrite: bool = False)

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 (defaults to False)


method move_file

move_file(source: Union[str, Path], destination: Union[str, Path], overwrite: bool = False)

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 (defaults to False)


method move_files

move_files(sources: List[Union[str, Path]], destination: Union[str, Path], overwrite: bool = False)

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


method normalize_path

normalize_path(path: Union[str, Path])

Removes redundant separators or up-level references from path.

  • Parameters: path – path that will be normalized
  • Returns: path to file as a string


method read_binary_file

read_binary_file(path: Union[str, Path])

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


method read_file

read_file(path: Union[str, Path], encoding: str = 'utf-8')

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

See Find Files for usage example.

Parameters
  • path – path to file to read
  • encoding – character encoding of file (default utf-8)
  • Returns: file content as string

method remove_directory

remove_directory(path: Union[str, Path], recursive: bool = False)

Removes the given directory, and optionally everything it contains.

Parameters
  • path – path to directory
  • recursive – remove all subdirectories and files (default to False)


method remove_file

remove_file(path: Union[str, Path], missing_ok: bool = True)

Removes the given file.

Parameters
  • path – path to the file to remove
  • missing_ok – ignore non-existent file (defaults to True)


method remove_files

remove_files(*paths: Union[str, Path], missing_ok: bool = True)

Removes multiple files.

Parameters
  • paths – paths to files to be removed
  • missing_ok – ignore non-existent files (default to True)


method run_keyword_if_file_exists

run_keyword_if_file_exists(path: Union[str, Path], keyword: str, *args)

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


method touch_file

touch_file(path: Union[str, Path])

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

method wait_until_created

wait_until_created(path: Union[str, Path], timeout: Union[int, float] = 5.0)

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


method wait_until_modified

wait_until_modified(path: Union[str, Path], timeout: Union[int, float] = 5.0)

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


method wait_until_removed

wait_until_removed(path: Union[str, Path], timeout: Union[int, float] = 5.0)

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