Reading and writing content
Chapter lab: Files and directories
This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch13-files.zip && unzip -q ch13-files.zip && rm ch13-files.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch13-files.zip -OutFile ch13-files.zip; Expand-Archive ch13-files.zip -DestinationPath .; Remove-Item ch13-files.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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”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} reviewedDone 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} checkedTask 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone 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.
How do you read a file's whole content?
Get File <path>returns the file's content as a string; assert on it withShould Contain.Show the answer
Answer:
Get File—Get File <path>returns the file's content as a string; assert on it withShould Contain.To check a file's content, you…
There is no
File Should Contain; read withGet Fileand assert withShould Contain.Show the answer
Answer:
Get FilethenShould Contain— There is noFile Should Contain; read withGet Fileand assert withShould Contain.
Key terms
- Get Filekeyword
- Read a file's content as a string.
- Create Filekeyword
- Write content to a file (creating it).