Course outline · 0% complete

0/28 lessons0%

Course overview →

Directories, metadata, and sizes

lesson 7-3 · ~10 min · 23/28

Directories are files about files

A directory (folder) is not a container that physically holds files. It is essentially a small table mapping names to files: "a.txt → file #4821, b.txt → file #4822". The filesystem tracks each file's content and metadata separately from its name. That design explains everyday behavior:

  • Moving a file within a disk is instant even for a 50 GB file: only the name entry moves, zero content bytes are copied.
  • A path like /Users/ada/mydata/a.txt is just directions: start at the root directory /, look up Users, then ada, then mydata, then a.txt.

Python's os module lets you create directories, list their name tables, and read any file's metadata with os.stat().

Code exercise · python

Run this. Create a directory, put two tiny files in it, list the directory's name table, and read one file's size from its metadata.

Code exercise · python

Your turn. Write "line 1\n" to "appendix.txt" in write mode ("w"), then ADD "line 2\n" using append mode ("a"), then print the whole file followed by its size in bytes. Append mode adds to the end instead of wiping the file.

Quiz

Moving a 50 GB file to another folder on the SAME disk takes under a second, but "moving" it to a USB drive takes minutes. Why?

Problem

os.stat("a.txt").st_size reports 1 for a file containing "x". Which of the two ingredients of a file does st_size come from: the content bytes or the metadata?