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
| Word | What it means | Example |
|---|---|---|
| File | One named piece of stored data. | lion.jpg |
| File-based storage | Keeping each set of data in its own file. | A CSV of students |
| CSV | A text file of rows, with commas between the values. | Sam,16,12.50 |
| Directory | A folder that holds files and other folders. | trips |
| Directory-based storage | Keeping files in named folders that show what is inside. | trips/2027/zoo |
| Path | The address of a file: its folders, then the file. | trips/2027/zoo/lion.jpg |
| Root | The top folder that holds all the others. | trips |
| File extension | The letters after the last dot, which show the file type. | .jpg |
| Subfolder | A 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 questionsDirectory-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
File-based and directory-based
| File-based | Directory-based | |
|---|---|---|
| Idea | Data in its own file | Files arranged in folders |
| Best for | Small amounts of data, settings, exports, logs | Many files, such as photos or documents |
| Finding things | Open the file and look | Follow the path through the folders |
| Good point | Simple, no extra software | Easy to organise, share and set who may open each folder |
| Weak point | Hard to search many files, and data may be repeated | A 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 questionsChanging 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
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
- The messy desktop. Report.docx. Report_final.docx. Report_final2.docx. New file.txt. Nobody can find anything.
- 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).
- 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).
- 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.
- 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.
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 questionsRemember
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.