File Paths: Relative, Absolute and Resolved Locations

Know where report.txt will be read

A script is stored in one directory but launched while the terminal is in another. A relative report path may refer to the launch directory, not automatically to the script’s directory.

A relative path is interpreted against a base location. An absolute path starts from a platform’s filesystem root. Path tools build and inspect paths according to the platform’s rules.

  1. Identify the base

    Explain current working directory versus script location.

  2. Build a path

    Use a path API rather than hard-coded separators.

  3. Separate location and permission

    Recognize normalization, existence and authorization as different questions.

The same relative name can select different locations

  1. Base

    Choose the directory deliberately.

    school/
  2. Relative child

    Combine with the report’s file name.

    reports/marks.txt
  3. Resolved target

    Interpret using the selected base and platform.

    school/reports/marks.txt
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

A relative path needs a stated base

Python open with a relative path uses the process’s current working directory. That may differ from the script directory. If a program needs script-relative files, choose that base explicitly rather than depending on launch context.

Roots and separators differ across platforms. Python pathlib and Node path APIs provide platform-aware operations. A joined path can still be missing or forbidden; constructing it performs no read.

Before starting, review real file operations. These examples use JavaScript; other languages can use different syntax and rules.

Build and inspect a filesystem path

This complete Python 3 example uses pathlib. It creates no file. join through / combines path parts according to the platform.

from pathlib import Path

report = Path('school') / 'reports' / 'marks.txt'
print(report.name)
print(report.is_absolute())
print(Path.cwd().is_absolute())

Expected output: marks.txt, False and True. cwd reports the current working directory. Its exact location depends on how the program was launched.

Compare relative and rooted resolution with a URL analogy

This JavaScript URL example illustrates base-relative versus root-relative selection. The first name resolves inside /reports/ while the rooted name starts at /.

It accesses no filesystem and does not claim URL rules equal operating-system path rules. The actual path API is shown before this analogy.

JavaScript · local teaching example

const base = new URL('https://school.example/reports/');
console.log(new URL('marks.txt', base).pathname);
console.log(new URL('/marks.txt', base).pathname);

Expected output

/reports/marks.txt
/marks.txt

Choose a script-relative directory explicitly

For a Python 3 script saved as path_report.py, __file__ identifies the script path. This program writes an exercise report beside that script, not at an arbitrary terminal launch directory.

from pathlib import Path

base = Path(__file__).resolve().parent
report = base / 'exercise-report.txt'
report.write_text('Maya: 75', encoding='utf-8')
print(report.read_text(encoding='utf-8'))

Expected output: Maya: 75. Run this only in a disposable directory where replacing exercise-report.txt is intended. Interactive environments may not define __file__.

Apply the rule to a small change

In the URL analogy, resolve ../marks.txt from /reports/daily/ and state which directory is selected.

Worked answer

The parent segment moves from daily to reports, so the URL path becomes /reports/marks.txt. Filesystem behavior needs the actual path API, including symbolic-link and platform details.

JavaScript · local teaching example

const base = new URL('https://school.example/reports/daily/');
console.log(new URL('../marks.txt', base).pathname);

Expected output

/reports/marks.txt

Recognize mistakes and limits

Joining arbitrary user input does not guarantee containment: parent segments, absolute paths and symbolic links can affect the final target. Define allowed locations and enforce them using the actual environment’s path and access rules.

Normalization does not establish that a file exists or that the current user is authorized to read it. Filesystem paths are not URL strings; encoding, roots and separators have different detailed rules.

For the language or tool rules used here, see Python filesystem paths.

Check the core idea

What is the normal base for Python open with a relative filename?
Choose one answer. You can try again.

Answer and explanation

The process current working directory.

Yes. It is not automatically the script’s folder.

Apply the idea to a new situation

Does joining a path prove it is safe to read?
Choose one answer. You can try again.

Answer and explanation

No; existence, containment and access rules still need checking.

Yes. Constructing a path performs no authorization guarantee.

Make the base and platform assumptions visible

Relative paths depend on a base; absolute paths name a rooted location. Use real path tools, choose launch-relative or script-relative behavior deliberately, and keep authorization separate from normalization.

Connect this idea with actual access outcomes and project boundaries.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Functions as Values →