Files, directories, and environment
Chapter lab: Files and directories
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
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
The OperatingSystem standard library checks the disk: whether files and
directories exist, what they contain, and the process environment. This part’s
workspace ships a sample_data/ folder of fixtures — a log, a CSV, config, and
XML — to work against. Keywords like File Should Exist, Directory Should Exist,
Set Environment Variable, and Environment Variable Should Be Set make good
preconditions: assert the data is there before a test uses it.
Task 1 — Assert the sample data files and an environment variable
Section titled “Task 1 — Assert the sample data files and an environment variable”Goal: create checks/files.robot that imports OperatingSystem, asserts the
sample data is present, sets an environment variable, and checks it is set.
Here is the same set of checks over a different file and variable. Adapt them.
Worked example — adapt it, don’t copy it
*** Settings ***Library OperatingSystem
*** Test Cases ***Log File Is Present File Should Exist sample_data/app.log Directory Should Exist sample_data Set Environment Variable SHOP_REGION eu Environment Variable Should Be Set SHOP_REGIONDone when
the suite imports OperatingSystem and defines Sample Data Is Present.
Hint 1
Assert two files this time: File Should Exist sample_data/catalog.xml and
File Should Exist sample_data/orders.csv.
Hint 2
Set and verify your own variable — pick any name, e.g.
Set Environment Variable SHOP_ENV test then
Environment Variable Should Be Set SHOP_ENV.
Solution
*** Settings ***Library OperatingSystem
*** Test Cases ***Sample Data Is Present File Should Exist sample_data/catalog.xml File Should Exist sample_data/orders.csv Directory Should Exist sample_data Set Environment Variable SHOP_ENV test Environment Variable Should Be Set SHOP_ENVTask 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone when
the files, directory, and environment variable all check out.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
Which library checks files and directories on disk?
The OperatingSystem standard library provides
File Should Exist,Directory Should Exist, environment keywords, and more.Show the answer
Answer: OperatingSystem — The OperatingSystem standard library provides
File Should Exist,Directory Should Exist, environment keywords, and more.File Should Exist sample_data/catalog.xmlfails when…It asserts the file exists; an empty-but-present file still passes.
Show the answer
Answer: the file is missing — It asserts the file exists; an empty-but-present file still passes.
Key terms
- OperatingSystemconcept
- Standard library for files, directories, and environment variables.
- File Should Existkeyword
- Assert a file exists on disk (Directory Should Exist for folders).