Course outline · 0% complete

0/28 lessons0%

Course overview →

What a file really is

lesson 7-1 · ~11 min · 21/28

Quiz

Warm-up from lesson 2-3: a text file contains "café". How many bytes does UTF-8 store on disk for it?

A file is a named bag of bytes

Strip away the icons and extensions: a file is just

  1. a sequence of bytes on disk (the content), and
  2. metadata the filesystem keeps about it: name, size, owner, permissions, timestamps.

That is all. A .txt, a .jpg, and a .py are the same kind of thing. The extension is a hint for humans and apps about how to interpret the bytes (remember from unit 2: bits mean nothing without an agreed rule). Renaming photo.jpg to photo.txt changes zero bytes of content.

The filesystem is the part of the OS that turns "the bytes of /Users/ada/notes.txt" into actual locations on the disk hardware, and it does that bookkeeping for millions of files.

Code exercise · python

Run this. Write two lines to a file, read them back, and count the characters. Note the \n newline characters are real bytes in the file and they count.

Code exercise · python

Your turn: prove a file is just bytes. Open "raw.bin" in binary write mode ("wb") and write bytes([72, 105, 33]). Then open it in binary read mode ("rb"), read the data, print it, and print data.decode("utf-8"). Those three numbers are the ASCII codes from lesson 2-3.

Quiz

You rename report.pdf to report.png. What is now true?