---
title: "Files and Folders"
book: "Primary & Middle School Computer Science"
subject: computer-science
language: en
chapter: 7
exercises: 11
source: https://one-course.com/books/computer-science/1/en/chapter/7-files-and-folders
license: CC-BY-NC-SA-4.0
credit: "One Computer Science Book, One Course (one-course.com)"
---

# Chapter 7 — Files and Folders

Leo wrote a poem last week, and today he wants to read it to the class. He switches on the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) and looks at the [desktop](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-desktop): it is covered with forty [icons](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-desktop), and most of them are called “untitled”. He opens one: a drawing. He opens another: a shopping list. The poem is somewhere in there, but where? It takes him twenty minutes to find it. In this chapter, we see how the work kept in a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) is organised, so that it can always be found again.

**You already know.**

[Saving](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-save) copies the work from the [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) into the [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage), where it is kept when the power is off; a good name says what the work is (Chapters [4](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#ch-g2-using-a-computer) and [6](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#ch-g3-inside-the-computer)).

## 7.1 A file holds one piece of work

**Definition 7.1 (File).**

A *file* is one piece of work kept in the [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage) of a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) under a name: a story, a drawing, a song, a film, a program. Its name is the *file name*.

Every time you save a new piece of work, the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) makes a new [file](#def-g3-files-and-folders-file). When you open a [file](#def-g3-files-and-folders-file), the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) copies it into the [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) so that you can see it and change it; when you save again, the [file](#def-g3-files-and-folders-file) in the [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage) is replaced by the new version.

## 7.2 Kinds of file and their extensions

The name of a [file](#def-g3-files-and-folders-file) usually ends with a dot followed by a few letters: `poem.txt`, `cat.jpg`, `song.mp3`.

**Definition 7.2 (Extension).**

The *extension* of a [file name](#def-g3-files-and-folders-file) is the end of the name, after the last dot. It tells what kind of [file](#def-g3-files-and-folders-file) it is.

| [extension](#def-g3-files-and-folders-extension) | kind of [file](#def-g3-files-and-folders-file) |
| --- | --- |
| `.txt` | text, with no colours or styles |
| `.png`, `.jpg` | a picture |
| `.mp3` | a sound or a song |
| `.mp4` | a film |
| `.py` | a program written in Python |

When you open a [file](#def-g3-files-and-folders-file), the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) looks at its [extension](#def-g3-files-and-folders-extension) to choose a tool that knows this kind of [file](#def-g3-files-and-folders-file): a picture viewer for `.jpg`, a music player for `.mp3`, a [text editor](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-word-processor) for `.txt`. The [extension](#def-g3-files-and-folders-extension) is only a label: if you change it, the inside of the [file](#def-g3-files-and-folders-file) stays exactly the same.

**Example 7.3 (A picture with the wrong label).**

Sami renames `boat.png` to `boat.txt`. The [file](#def-g3-files-and-folders-file) still holds the same picture, but now the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) opens it with the [text editor](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-word-processor), which shows a mess of strange signs. When Sami gives back the name `boat.png`, the picture opens again, unchanged.

A [file](#def-g3-files-and-folders-file) can also be made *read-only*: it can then be opened and read, but not changed. Later in this course, we see who is allowed to read or change which [files](#def-g3-files-and-folders-file).

## 7.3 Folders inside folders

**Definition 7.4 (Folder).**

A *folder* is a place in the [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage), with a name, that holds [files](#def-g3-files-and-folders-file) and other folders.

![A tidy desk: papers in folders, folders in a box. A computer keeps its files the same way.](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/img-59e3f1886bc0.jpg)

*A tidy desk: papers in [folders](#def-g3-files-and-folders-folder), [folders](#def-g3-files-and-folders-folder) in a box. A [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) keeps its [files](#def-g3-files-and-folders-file) the same way.*

A [folder](#def-g3-files-and-folders-folder) can hold other [folders](#def-g3-files-and-folders-folder), which can hold other [folders](#def-g3-files-and-folders-folder) again. Leo puts his school work in a [folder](#def-g3-files-and-folders-folder) `School`, inside his [folder](#def-g3-files-and-folders-folder) `Documents`; inside `School`, there is a [folder](#def-g3-files-and-folders-folder) for each subject. All the [folders](#def-g3-files-and-folders-folder) together, one inside the other, make a tree.

**Definition 7.5 (Folder tree).**

The *folder tree* of a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) is the drawing of all its [folders](#def-g3-files-and-folders-folder) and [files](#def-g3-files-and-folders-file), each one below the [folder](#def-g3-files-and-folders-folder) that holds it, with one [folder](#def-g3-files-and-folders-folder) at the top that holds all the others.

The [folder tree](#def-g3-files-and-folders-tree) is a tree drawn upside down: its root is at the top, and its branches spread downwards. The [files](#def-g3-files-and-folders-file) are at the ends of the branches.

![Leo’s folder tree. The yellow boxes are folders, the white boxes are files.](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/fig-bf65a80d0b9e.svg)

*Leo’s [folder tree](#def-g3-files-and-folders-tree). The yellow boxes are [folders](#def-g3-files-and-folders-folder), the white boxes are [files](#def-g3-files-and-folders-file).*

## 7.4 The path to a file

To tell somebody where a [file](#def-g3-files-and-folders-file) is, we give the [folders](#def-g3-files-and-folders-folder) to open one after the other, from the top of the tree down to the [file](#def-g3-files-and-folders-file).

**Definition 7.6 (File path).**

The *file path* of a [file](#def-g3-files-and-folders-file) is the list of the [folders](#def-g3-files-and-folders-folder) from the top of the [folder tree](#def-g3-files-and-folders-tree) down to the [file](#def-g3-files-and-folders-file), followed by the [file name](#def-g3-files-and-folders-file), separated by the sign `/`.

![Leo/Documents/School/Poems/poem.txt The path to Leo’s poem, from the top of the tree down to the file.](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/fig-676b136ed315.svg)

*`Leo/Documents/School/Poems/poem.txt` The path to Leo’s poem, from the top of the tree down to the [file](#def-g3-files-and-folders-file).*

**Method 7.7 (Reading a path).**

Start at the [folder](#def-g3-files-and-folders-folder) at the top of the tree. Read the path from left to right: each name before a `/` is a [folder](#def-g3-files-and-folders-folder) to open, inside the one before it. The last name, after the last `/`, is the [file](#def-g3-files-and-folders-file).

Most [computers](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) write the paths with `/`; some write them with the other slant, `\`. [File](#def-g3-files-and-folders-file) managers show the path of the open [folder](#def-g3-files-and-folders-folder) at the top of their window.

![A file manager showing what is inside the folder Pictures: each file as an icon, with its name and extension below it.](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/fig-9310b6a72864.svg)

*A [file](#def-g3-files-and-folders-file) manager showing what is inside the [folder](#def-g3-files-and-folders-folder) `Pictures`: each [file](#def-g3-files-and-folders-file) as an [icon](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-desktop), with its name and [extension](#def-g3-files-and-folders-extension) below it.*

## 7.5 Copying, moving, renaming, deleting

**Definition 7.8 (Copy and move).**

To *copy* a [file](#def-g3-files-and-folders-file) into a [folder](#def-g3-files-and-folders-folder) is to make a second [file](#def-g3-files-and-folders-file), the same as the first, in that [folder](#def-g3-files-and-folders-folder): then there are two. To *move* a [file](#def-g3-files-and-folders-file) into a [folder](#def-g3-files-and-folders-folder) is to take it out of its [folder](#def-g3-files-and-folders-folder) and put it in the other one: there is still only one.

![Leo copies his poem into the folder Best: now there are two poems. If he moves it instead, the poem leaves Poems: there is still only one.](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/fig-304a643e4af7.svg)

*Leo copies his poem into the [folder](#def-g3-files-and-folders-folder) `Best`: now there are two poems. If he moves it instead, the poem leaves `Poems`: there is still only one.*

To *rename* a [file](#def-g3-files-and-folders-file) is to change its name; the [file](#def-g3-files-and-folders-file) stays where it is, unchanged. In a [folder](#def-g3-files-and-folders-folder), two [files](#def-g3-files-and-folders-file) can never have the same name, or the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) would not know which one you mean.

**Definition 7.9 (Bin).**

The *bin* is a special [folder](#def-g3-files-and-folders-folder) where deleted [files](#def-g3-files-and-folders-file) go. A [file](#def-g3-files-and-folders-file) in the bin can still be put back where it was; once the bin is emptied, its [files](#def-g3-files-and-folders-file) are gone for good.

**In practice — A folder plan.**

Before saving, choose the [folder](#def-g3-files-and-folders-folder). Make one [folder](#def-g3-files-and-folders-folder) per subject, or per project, with a short, clear name; inside, give each [file](#def-g3-files-and-folders-file) a name that says what it is, like `poem-the-rain.txt`. Empty the [bin](#def-g3-files-and-folders-bin) only when you are sure that nothing in it is needed.

## 7.6 Exercises

**Exercise 7.1 ★.**

What kind of [file](#def-g3-files-and-folders-file) is each one? `map.png`, `notes.txt`, `dance.mp4`, `game.py`, `song.mp3`, `photo.jpg`, `list.txt`, `film.mp4`.

**Solution of Exercise 7.1.**

`map.png`: a picture. `notes.txt`: text. `dance.mp4`: a film. `game.py`: a program written in Python. `song.mp3`: a sound. `photo.jpg`: a picture. `list.txt`: text. `film.mp4`: a film.

**Exercise 7.2 ★.**

Write the path to the [file](#def-g3-files-and-folders-file) `sums.txt` in Leo’s [folder tree](#def-g3-files-and-folders-tree).

**Solution of Exercise 7.2.**

`Leo/Documents/School/Maths/sums.txt`.

**Exercise 7.3 ★.**

Which [folder](#def-g3-files-and-folders-folder) holds the [file](#def-g3-files-and-folders-file) `Leo/Pictures/rocket.png`? And the [file](#def-g3-files-and-folders-file) `Leo/Documents/letter.txt`?

**Solution of Exercise 7.3.**

The [folder](#def-g3-files-and-folders-folder) `Pictures`, inside `Leo`; the [folder](#def-g3-files-and-folders-folder) `Documents`, inside `Leo`.

**Exercise 7.4 ★.**

Copy or move? (a) Mia puts her story on a [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) stick for her teacher, and keeps it on her [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) too. (b) Tom saved a picture in the wrong [folder](#def-g3-files-and-folders-folder). (c) Lina wants a spare copy of her work in another [folder](#def-g3-files-and-folders-folder), just in case.

**Solution of Exercise 7.4.**

(a) Copy: she keeps her own [file](#def-g3-files-and-folders-file). (b) Move: the picture should be in one place only, the right [folder](#def-g3-files-and-folders-folder). (c) Copy.

**Exercise 7.5 ★.**

Where does a [file](#def-g3-files-and-folders-file) go when you delete it? Can it come back?

**Solution of Exercise 7.5.**

Into the [bin](#def-g3-files-and-folders-bin). It can be put back where it was, as long as the [bin](#def-g3-files-and-folders-bin) has not been emptied.

**Exercise 7.6 ★.**

Draw the [folder tree](#def-g3-files-and-folders-tree): the [folder](#def-g3-files-and-folders-folder) `Mia` holds two [folders](#def-g3-files-and-folders-folder), `Drawings` and `Stories`; `Drawings` holds `cat.png` and `house.png`; `Stories` holds `dragon.txt`.

**Solution of Exercise 7.6.**

![](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/fig-32c38232a0a8.svg)

**Exercise 7.7 ★.**

Make a [folder](#def-g3-files-and-folders-folder) plan for your school year: the [folders](#def-g3-files-and-folders-folder) you would create, and where they would go in the tree.

**Solution of Exercise 7.7.**

Many plans are good. For example: a [folder](#def-g3-files-and-folders-folder) `School year` at the top, holding one [folder](#def-g3-files-and-folders-folder) per subject (`Reading`, `Maths`, `Science`, `Art`…), and inside each subject one [folder](#def-g3-files-and-folders-folder) per project when there are many [files](#def-g3-files-and-folders-file). Each [file](#def-g3-files-and-folders-file) gets a name that says what it is.

**Exercise 7.8 ★.**

How many [folders](#def-g3-files-and-folders-folder) and how many [files](#def-g3-files-and-folders-file) are there in Leo’s [folder tree](#def-g3-files-and-folders-tree)?

**Solution of Exercise 7.8.**

7 [folders](#def-g3-files-and-folders-folder) (`Leo`, `Documents`, `School`, `Poems`, `Maths`, `Pictures`, `Music`) and 8 [files](#def-g3-files-and-folders-file) (`poem.txt`, `rain.txt`, `sums.txt`, `letter.txt`, `cat.jpg`, `beach.png`, `rocket.png`, `song.mp3`).

**Exercise 7.9 ★★.**

Leo renames `poem.txt` to `poem.jpg`. What happens when he opens it? Is his poem lost?

**Solution of Exercise 7.9.**

The [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) thinks it is a picture and opens it with a picture viewer, which cannot show it: it shows an error or nothing. The poem is not lost: the inside of the [file](#def-g3-files-and-folders-file) has not changed. Renaming it back to `poem.txt` brings it back.

**Exercise 7.10 ★★.**

Two [files](#def-g3-files-and-folders-file) called `notes.txt` can be kept in two different [folders](#def-g3-files-and-folders-folder), but not in the same [folder](#def-g3-files-and-folders-folder). Why?

**Solution of Exercise 7.10.**

Their paths are different, for example `Leo/Documents/notes.txt` and `Leo/Music/notes.txt`, so the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) can tell them apart. In one [folder](#def-g3-files-and-folders-folder), two [files](#def-g3-files-and-folders-file) with the same name would have the same path, and the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) would not know which one you mean.

**Exercise 7.11 ★★.**

On Sami’s [desktop](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-desktop) there are three [folders](#def-g3-files-and-folders-folder), `Pictures`, `Writing` and `Poems`, and three [files](#def-g3-files-and-folders-file), `cat.jpg`, `dog.jpg` and `story.txt`. He wants `Poems` and `story.txt` inside `Writing`, and both pictures inside `Pictures`. Each move moves one [icon](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-desktop), and a [folder](#def-g3-files-and-folders-folder) moves with everything inside it. What is the smallest number of moves? Draw his tree afterwards.

**Solution of Exercise 7.11.**

Four moves: `Poems` into `Writing` (its poems go with it), `story.txt` into `Writing`, `cat.jpg` and `dog.jpg` into `Pictures`. Each of the four [icons](https://one-course.com/books/computer-science/1/en/chapter/4-using-a-computer#def-g2-using-a-computer-desktop) that must change place needs one move, so fewer is impossible.

![](https://one-course.com/images/onecourse/chapters/computer-science-1/g3-files-and-folders/fig-fb6dfba7784c.svg)
