Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

6.6 (1 of 3): File-based and directory-based storage

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

40 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

The messy desktop

Look at this desktop. Report.docx. Report_final.docx. Report_final2.docx. Photo1.jpg. Photo2.jpg. New file.txt.

Nobody can find anything. Now imagine 10,000 files like this.

You need a clear way to store data. This lesson shows two ways: files, and folders that hold files.

Key words

WordWhat it meansExample
FileOne named piece of stored data.lion.jpg
File-based storageKeeping each set of data in its own file.A CSV of students
CSVA text file of rows, with commas between the values.Sam,16,12.50
DirectoryA folder that holds files and other folders.trips
Directory-based storageKeeping files in named folders that show what is inside.trips/2027/zoo
PathThe address of a file: its folders, then the file.trips/2027/zoo/lion.jpg
RootThe top folder that holds all the others.trips
File extensionThe letters after the last dot, which show the file type..jpg
SubfolderA folder inside another folder.2027 inside trips

The cells students fill in are left blank in this preview.

File-based storage

In file-based storage, each set of data lives in its own file. A program opens the file, reads it and saves it.

It is simple. A notes file, a CSV of marks and a settings file are all file-based. But the files know nothing about each other. If the same name is in two files, you must change both.

Quick check: files

Have a go

Quick check: files

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Directory-based storage

In directory-based storage, files go in folders. Folders go inside other folders. The names of the folders tell you what is inside.

You find a file by its path. The path trips/2027/zoo/photos/lion.jpg means: the trips folder, then 2027, then zoo, then photos, then the file.

A folder tree for school trips

A folder tree with "trips: root folder" at the top, holding "2027" and "2028"; "2027" holds "zoo" and "museum"; "zoo" holds "photos" and "permission.pdf"; "photos" holds "lion.jpg" and "zebra.jpg"; "museum" holds "mummy.jpg"; and a key shows that a folder is a box with a tab and a file a box without one.A folder tree with "trips: root folder" at the top, holding "2027" and "2028"; "2027" holds "zoo" and "museum"; "zoo" holds "photos" and "permission.pdf"; "photos" holds "lion.jpg" and "zebra.jpg"; "museum" holds "mummy.jpg"; and a key shows that a folder is a box with a tab and a file a box without one.

File-based and directory-based

File-basedDirectory-based
IdeaData in its own fileFiles arranged in folders
Best forSmall amounts of data, settings, exports, logsMany files, such as photos or documents
Finding thingsOpen the file and lookFollow the path through the folders
Good pointSimple, no extra softwareEasy to organise, share and set who may open each folder
Weak pointHard to search many files, and data may be repeatedA wrong folder name hides a file

The cells students fill in are left blank in this preview.

Which storage would you choose?

Have a go

Which storage would you choose?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Changing the structure

Note

You can change one structure into another. Read one big CSV file of all trips, then save one file for each year in a folder for that year. The data stays the same. Only the way it is stored changes. Then a person can find one year fast.

Try it: take a path apart

Code: python
path = "trips/2027/zoo/photos/lion.jpg"

parts = path.split("/")
print(parts)

file = parts.pop()      # pop takes the last part off the list
print("File:", file)
print("Folders:", parts)

# Change the path to your own folder and run it again.

Make a free account to save your work and track your progress.

Match the path part to its job

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Watch: file-based and directory-based storage

Press Play on the short film below. It starts with a messy desktop. Then it shows files, folders and a path, and ends with a myth.

Film: file-based and directory-based storage

File-based and directory-based storage

Every scene, as text

  1. The messy desktop. Report.docx. Report_final.docx. Report_final2.docx. New file.txt. Nobody can find anything.
  2. File-based storage. Each set of data lives in its own file (good point: simple, no extra software). A notes file, a CSV of marks and a settings file. If the same name is in two files, you must change both (weak point: data may be repeated).
  3. Directory-based storage. trips/2027/zoo/photos/lion.jpg (path: the address of a file). trips (root: the top folder), then 2027 (folder: a box with a tab), then zoo, then photos, then lion.jpg (file: at the end of the path).
  4. File-based and directory-based. File-based storage (best for: small amounts of data). Directory-based storage (best for: many files, such as photos; good point: easy to organise and share; good point: set who may open each folder). Choose folders when you have many files.
  5. Myth (false): a folder is a file. File: one named piece of stored data. That is not true: a folder holds files, but is not a file. Directory: a folder that holds files and other folders.

Your turn: work with paths

Paths are plain text with a slash between the parts. Fill in the five functions. Nothing here opens a real file. Press Run Tests to check.

Code: python
def file_name(path):
    # path looks like "trips/2027/zoo/photos/lion.jpg"
    # Give back only the last part: "lion.jpg"
    return None


def extension(path):
    # Give back the file type after the last dot, without the dot: "jpg"
    # Give back "" if there is no dot.
    return None


def folder_of(path):
    # Give back the folder that holds the file: "trips/2027/zoo/photos"
    return None


def join_path(parts):
    # parts is a list such as ["trips", "2027", "zoo"].
    # Give back "trips/2027/zoo".
    return None


def count_in_folder(paths, folder):
    # Count how many paths are directly in that folder.
    # A file in a deeper subfolder does not count.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Your turn: draw a folder tree

A club has photos, a list of members and money records for 2027 and 2028. Change the diagram so it shows a good folder tree for the club. Use at least three levels of folders. Name each folder clearly.

flowchart TD
  club[club]
  club --> a[Change me]
  club --> b[Change me too]

Make a free account to save your work, or press Start lab to work through the whole lesson.

Good folder names

Tip

Use clear names. Use the same pattern each time, such as year then place. Avoid names like New folder and final2. A good name means you do not need to open the file to know what it is.

Final check

Have a go

Final check

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

Important

File-based: data in its own file, simple. Directory-based: files in folders, found by a path. Choose folders when you have many files.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.