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.
-
name
¶ Alias for field number 1
-
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.
-
mtime
¶ Alias for field number 3
-
name
¶ Alias for field number 1
-
path
¶ Alias for field number 0
-
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)¶ Returns the absolute path to a file, and resolves symlinks.
- Parameters
path – path that will be resolved
- Returns
absolute path to file
-
append_to_binary_file
(path, content)¶ 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')¶ 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)¶ 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)¶ Copy directory from source path to destination path.
- Parameters
source – path to source directory
destination – path to copy destination
-
copy_file
(source, destination)¶ Copy a file from source path to destination path.
- Parameters
source – path to source file
destination – path to copy destination
-
copy_files
(sources, destination)¶ 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)¶ 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)¶ 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)¶ 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)¶ Returns True if the given directory exists, False if not.
- Parameters
path – path to inspected directory
-
does_directory_not_exist
(path)¶ Returns True if the directory does not exist, False if it does.
- Parameters
path – path to inspected directory
-
does_file_exist
(path)¶ Returns True if the given file exists, False if not.
- Parameters
path – path to inspected file
-
does_file_not_exist
(path)¶ Returns True if the file does not exist, False if it does.
- Parameters
path – path to inspected file
-
empty_directory
(path)¶ Removes all the files in the given directory.
- Parameters
path – directory to remove files from
-
find_files
(pattern, include_dirs=True, include_files=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
include_files – include files in results
- Returns
list of paths that match the pattern
-
get_file_creation_date
(path)¶ Returns the creation time in seconds. Note: Linux sets this whenever file metadata changes
- Parameters
path – path to file to inspect
-
get_file_extension
(path)¶ Returns the suffix for the file.
- Parameters
path – path to file
-
get_file_modified_date
(path)¶ Returns the modified time in seconds.
- Parameters
path – path to file to inspect
-
get_file_name
(path)¶ Returns only the filename portion of a path.
- Parameters
path – path to file
-
get_file_owner
(path)¶ Return the name of the user who owns the file.
- Parameters
path – path to file to inspect
-
get_file_size
(path)¶ Returns the file size in bytes.
- Parameters
path – path to file to inspect
-
is_directory_empty
(path=None)¶ Returns True if the given directory has no files or subdirectories.
- Parameters
path – path to inspected directory
-
is_directory_not_empty
(path=None)¶ Returns True if the given directory has any files or subdirectories.
- Parameters
path – path to inspected directory
-
is_file_empty
(path)¶ Returns True if the given file has no content, i.e. has zero size.
- Parameters
path – path to inspected file
-
is_file_not_empty
(path)¶ Returns True if the given file has content, i.e. larger than zero size.
- Parameters
path – path to inspected file
-
join_path
(*parts)¶ Joins multiple parts of a path together.
- Parameters
parts – Components of the path, e.g. dir, subdir, filename.ext
-
list_directories_in_directory
(path=None)¶ Lists all the directories in the given directory, relative to it.
- Parameters
path – base directory for search, defaults to current working dir
-
list_files_in_directory
(path=None)¶ Lists all the files in the given directory, relative to it.
- Parameters
path – base directory for search, defaults to current working dir
-
log_directory_tree
(path=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)¶ 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)¶ 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)¶ 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)¶ Removes redundant separators or up-level references from path.
- Parameters
path – path that will be normalized
- Returns
path to file
-
read_binary_file
(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
-
read_file
(path, encoding='utf-8')¶ Reads a file as text, with given encoding, and returns the content.”
- Parameters
path – path to file to read
encoding – character encoding of file
-
remove_directory
(path, recursive=False)¶ 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)¶ Removes the given file.
- Parameters
path – path to the file to remove
missing_ok – ignore non-existent file
-
remove_files
(*paths, missing_ok=True)¶ 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)¶ 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)¶ 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)¶ Poll path until it exists, or raise exception if timeout is reached.
- Parameters
path – path to poll
timeout – time in seconds until keyword fails
-
wait_until_modified
(path, timeout=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
-
wait_until_removed
(path, timeout=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
-