FileUtils

Robot Framework library documentation

FileUtils Documentation

Version 1.2.0 Scope GLOBAL 14 Keywords
Author: Jane SmithMaintainer: Infrastructure TeamLicense: Apache-2.0Robot Framework: >=7.0Python: >=3.9
Sample Usage
Robot
*** Settings ***
Library    FileUtils

*** Test Cases ***
Example
    [Documentation]    Demonstrates using FileUtils
    # add your keyword calls here

Introduction

File and directory utilities for Robot Framework.

This library provides comprehensive file system operations including:

  • File reading and writing
  • Directory creation and management
  • File copying and moving
  • Path manipulation
  • File existence checks

Arguments

file_path : str
encoding : str default: "utf-8"

Return Type

str

Documentation

Read entire content of a file.

Arguments:

  • file_path: Path to the file to read
  • encoding: File encoding (default: utf-8)

Returns: File content as string

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Read File Example
    ${content}    Read File Content    /path/to/file.txt
    Should Not Be Empty     ${content}

Arguments

file_path : str
content : str
encoding : str default: "utf-8"

Documentation

Write content to a file.

Creates the file if it doesn't exist, overwrites if it does.

Arguments:

  • file_path: Path where to write the file
  • content: Content to write
  • encoding: File encoding (default: utf-8)

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Write File Example
    Write File Content     output.txt    Hello World
    File Should Exist     output.txt

Arguments

file_path : str

Return Type

bool

Documentation

Check if a file exists.

Arguments:

  • file_path: Path to check

Returns: True if file exists, False otherwise

Arguments

dir_path : str

Return Type

bool

Documentation

Check if a directory exists.

Arguments:

  • dir_path: Directory path to check

Returns: True if directory exists, False otherwise

Arguments

dir_path : str
parents : bool default: True

Documentation

Create a directory.

Arguments:

  • dir_path: Directory path to create
  • parents: Create parent directories if needed (default: True)

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Create Directory Example
    Create Directory     /path/to/new/directory
    Directory Should Exist     /path/to/new/directory

Arguments

source : str
destination : str

Documentation

Copy a file from source to destination.

Arguments:

  • source: Source file path
  • destination: Destination file path

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Copy File Example
    Copy File     source.txt    backup/source.txt
    File Should Exist     backup/source.txt

Arguments

source : str
destination : str

Documentation

Move a file from source to destination.

Arguments:

  • source: Source file path
  • destination: Destination file path

Arguments

file_path : str

Documentation

Delete a file.

Arguments:

  • file_path: Path to file to delete

Raises: FileNotFoundError if file doesn't exist

Arguments

directory : str
pattern : Optional[str] default: None

Return Type

List[str]

Documentation

List all files in a directory.

Arguments:

  • directory: Directory path to list
  • pattern: Optional glob pattern to filter files (e.g., "*.txt")

Returns: List of file paths

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
List Files Example
    ${files}    List Files    /path/to/dir    *.txt
    Should Not Be Empty     ${files}

Arguments

file_path : str

Return Type

int

Documentation

Get file size in bytes.

Arguments:

  • file_path: Path to file

Returns: File size in bytes

Arguments

file_path : str

Return Type

dict

Documentation

Read and parse a JSON file.

Arguments:

  • file_path: Path to JSON file

Returns: Parsed JSON as dictionary

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Read JSON File Example
    ${data}    Read Json File    config.json
    Should Be Equal     ${data}[key]    value

Arguments

file_path : str
data : dict
indent : int default: 2

Documentation

Write data to a JSON file.

Arguments:

  • file_path: Path where to write JSON file
  • data: Dictionary to write as JSON
  • indent: JSON indentation (default: 2)

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Write JSON File Example
    ${config}    Create Dictionary    key=value    name=test
    Write Json File     config.json    ${config}
    File Should Exist     config.json

Arguments

file_path : str

Return Type

str

Documentation

Get file extension.

Arguments:

  • file_path: File path

Returns: File extension (without dot)

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Get File Extension Example
    ${ext}    Get File Extension    document.pdf
    Should Be Equal     ${ext}    pdf

Arguments

*paths : str

Return Type

str

Documentation

Join multiple path components.

Arguments:

  • *paths: Variable number of path components

Returns: Joined path string

Example:

*** Settings ***
Library    FileUtils


*** Test Cases ***
Join Paths Example
    ${path}    Join Paths    /home    user    documents    file.txt
    Should Be Equal     ${path}    /home/user/documents/file.txt

Need Help?

Found an issue or have a feature request for this library? Let the maintainers know by opening a new GitHub issue. Please include environment details and relevant log output to help us reproduce the problem.

Open an Issue on GitHub