Everyone Can Learn Program
中文 Start learning
Course 0 · Before codeLesson 5 of 8Free to read

Files and folders · Computer Basics

Where does a photo go when you save it, and how do you find it again?

Course contents

Why you need it

Storage holds millions of things. Files and folders are how you, and every program, keep them in order and find them again.

Later your programs will open files by their path, and the Linux course is all about moving between folders. Knowing the idea now makes all of that easy.

The intuition

A file is one saved thing with a name: a photo, a song, a document, a program. A folder (also called a directory) is a container that holds files and other folders. Folders inside folders make a tree:

/                 the top
└── Users
    └── mia
        ├── Documents
        │   └── cv.docx
        └── Pictures
            ├── beach.jpg
            └── 2024
                └── cat.png

To say exactly where a file is, write its path: the folders from the top down to it, separated by /:

/Users/mia/Pictures/2024/cat.png

It's like a postal address read from the country down to the house number. On Windows a path starts with a drive letter and uses backslashes, like C:\Users\mia\Pictures\cat.png, but it's the same idea.

File extensions. The part after the last dot says what kind of file it is: .jpg and .png are pictures, .mp3 is sound, .mp4 is video, .pdf and .docx are documents, .txt is plain text and .py is a Python program. Inside, every file is just bytes; the extension tells programs how to read them.

Definition

File

A file is a named collection of data kept in storage, such as a photo, a document or a program.

Folder (directory)

A folder, or directory, is a container that holds files and other folders.

Path

A path is the address of a file or folder: the folders from the top down to it, separated by slashes.

Examples

Build a path

Try it yourself

Put the folders in order from the top, then the file name. Add a folder to the list (in quotes, with a comma) and see the path grow.

folders = ["Users", "mia", "Pictures"]   # change me 改这里
file = "cat.png"
path = ""
for name in folders:
    path = path + "/" + name
path = path + "/" + file
print(path)
Step by step
  1. L1The folders, from the top down.["Users", "mia", "Pictures"]
  2. L5Add each folder with a / in front.path = /Users
  3. L5Keep going down the tree.path = /Users/mia/Pictures
  4. L6The file name goes last.path = /Users/mia/Pictures/cat.png

Draw a path as a tree

Try it yourself

The opposite job: cut a path at every / and draw each part one level deeper. Paste in a path of your own.

path = "/Users/mia/Documents/school/report.docx"   # change me 改这里
parts = path.split("/")[1:]
depth = 0
for part in parts[:-1]:
    print("  " * depth + part + "/")
    depth = depth + 1
print("  " * depth + parts[-1])
Step by step
  1. L2Cut the path at every /.Users | mia | Documents | school | report.docx
  2. L5Every folder is drawn one level deeper than the one before.Users/
  3. L7The file sits inside the last folder.report.docx

What kind of file?

Try it yourself

The machine looks only at the extension, the part after the last dot. Try song.mp3, notes.txt or game.exe.

name = "holiday.mp4"   # change me 改这里
kinds = {"jpg": "a picture", "png": "a picture", "mp3": "sound",
         "mp4": "a video", "txt": "plain text", "pdf": "a document",
         "py": "a Python program"}
extension = name.split(".")[-1]
print(name, "is", kinds.get(extension, "a kind of file this machine doesn't know"))
Step by step
  1. L1The file name.name = "holiday.mp4"
  2. L5Take the part after the last dot.extension = "mp4"
  3. L6Look it up in the table.holiday.mp4 is a video

Common mistakes

Renaming the extension doesn't convert the file

Renaming song.mp3 to song.mp4 doesn't turn music into a video; the bytes inside stay the same. It only confuses programs about how to open it.

Same name, different folders

/Users/mia/notes.txt and /Users/sam/notes.txt are two different files: the path, not just the name, says which file you mean. Inside one folder, though, every name must be different.

Hidden extensions

Many computers hide extensions, so photo.jpg shows up as just photo. Turn on "show file name extensions" in your file manager's settings to see them.

Quick quiz

1. In /Users/mia/Music/song.mp3, which folder is song.mp3 directly inside?

  1. AUsers
  2. Bmia
  3. CMusic
Show the answer

Answer: C

The folder just before the file name holds it: Music. mia holds Music, and Users holds mia.

2. Which file is most likely a picture?

  1. Anotes.txt
  2. Bbeach.jpg
  3. Ctrack.mp3
Show the answer

Answer: B

.jpg is a picture format. .txt is plain text and .mp3 is sound.

3. Can two files both be called notes.txt?

  1. ANever
  2. BYes, if they are in different folders
  3. CYes, even in the same folder
Show the answer

Answer: B

Different folders give them different paths, so the computer can tell them apart. In the same folder they would clash.

Key points

  • A file is saved data with a name; folders hold files and other folders, forming a tree.
  • A path like /Users/mia/Pictures/cat.png says exactly where a file is.
  • The extension (.jpg, .mp3, .pdf …) says what kind of file it is.

Practice

3 short exercises: think it through, change something and see what happens, then do one yourself. Answers are checked the moment you submit.

  1. ThinkFind the path

    Here is part of a computer's folder tree:

    /
    └── home
        └── leo
            ├── games
            │   └── chess.py
            └── music
                └── rain.mp3

    What is the path of rain.mp3?

  2. ChangeThe photo moved

    Mia made a new folder called pets inside Pictures and moved cat.png into it.

    Change the folder list so the machine prints the new path:

    /Users/mia/Pictures/pets/cat.png
  3. Your turnWrite the path

    Here is Ana's computer:

    /
    └── Users
        └── ana
            ├── Desktop
            └── Documents
                └── 2025
                    └── taxes.pdf

    Write the full path of taxes.pdf.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
File文件A named piece of data kept in storage.
Folder (directory)文件夹(目录)A container that holds files and other folders.
Path路径The address of a file: the folders from the top down to it, separated by slashes.
File extension扩展名The end of a file name after the last dot, such as .jpg, which says what kind of file it is.