Skip to content

Reading and writing content

Chapter lab: Files and directoriesch13-files.zip · 6 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch13-files.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch13-files.zip && unzip -q ch13-files.zip && rm ch13-files.zip

Open the folder in VS Code with code rfe-ch13-files (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch13-files/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorysample_data/
      • app.log
      • catalog.xml
      • orders.csv
      • results.xml
      • settings.cfg
    • Directorytools/
      • inventory.py
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

To check what a file contains, read it with Get File and assert on the string with Should Contain. There is no File Should Contain keyword — this is a common newcomer trap. To produce a file, Create File writes content. Write into ${OUTPUT_DIR}, the run’s output folder — a safe place that won’t clutter your workspace.

Task 1 — Read a log file and write a summary

Section titled “Task 1 — Read a log file and write a summary”
Edit

Goal: create checks/readwrite.robot that reads a bundled file with Get File, asserts on its contents, writes a file with Create File, and reads it back to confirm.

Here is the same read-then-write flow over a different file. Adapt it.

Worked example — adapt it, don’t copy it

*** Settings ***
Library OperatingSystem
*** Test Cases ***
Copy A Value Into A Note
${orders}= Get File sample_data/orders.csv
Should Contain ${orders} travel-mug
Create File ${OUTPUT_DIR}/note.txt reviewed
${note}= Get File ${OUTPUT_DIR}/note.txt
Should Contain ${note} reviewed

Done when

the suite defines Read And Write File Content.

Hint 1

Read the log with ${log}= Get File sample_data/app.log, then Should Contain ${log} ERROR — the log has error lines in it.

Hint 2

Write with Create File ${OUTPUT_DIR}/summary.txt checked, then read it back with Get File and Should Contain ${written} checked.

Solution
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Read And Write File Content
${log}= Get File sample_data/app.log
Should Contain ${log} ERROR
Create File ${OUTPUT_DIR}/summary.txt checked
${written}= Get File ${OUTPUT_DIR}/summary.txt
Should Contain ${written} checked
Run

Run:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the log is read and the summary file is written and verified.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. How do you read a file's whole content?

    Show the answer

    Answer: Get File — Get File <path> returns the file's content as a string; assert on it with Should Contain.

  2. To check a file's content, you…

    Show the answer

    Answer: Get File then Should Contain — There is no File Should Contain; read with Get File and assert with Should Contain.

Key terms

Get Filekeyword
Read a file's content as a string.
Create Filekeyword
Write content to a file (creating it).

All terms in the glossary →