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.
PATH_TYPE
alias of Union
[str
, Path
]
variable ROBOT_LIBRARY_DOC_FORMAT
variable ROBOT_LIBRARY_SCOPE
method absolute_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
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
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
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 from source path to destination path.
Parameters
- source β path to source directory
- destination β path to copy destination
method copy_file
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 multiple files to destination folder.
Parameters
- sources β list of source files
- destination β path to destination folder
method create_binary_file
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
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
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
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
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
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
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
Removes all the files in the given directory.
- Parameters: path β directory to remove files from
method find_files
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
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
Returns the suffix for the file.
- Parameters: path β path to file
- Returns: file suffix as a string
method get_file_modified_date
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
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
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
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
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
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
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
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
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
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
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
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
Logs all the files in the directory recursively.
- Parameters: path β base directory to start from, defaults to current working dir
method move_directory
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 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 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
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
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
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
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
Removes the given file.
Parameters
- path β path to the file to remove
- missing_ok β ignore non-existent file (defaults to
True
)
method remove_files
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
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
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
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
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
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