Files, Folders, and Path Names

In these notes you will learn:

  • How to name files and folders using / notation.
  • The difference between absolute path names and relative path names.
  • How the files and folders of a Processing program are organized.

Files and Folders

Data stored on disk is usually organized into files and folders (or directories), know as a file system. A file is a named collection of on-disk data, while a folder is a named collection of other files and folders. The folders in a file system form a hierarchy that is usually imagined as an upside-down tree.

For example, here is a file system with four folders and three files:

A simple example file system.

One way to name files is to use their absolute path name. This is the name of the file preceded by the names of the folders it is contained within. For example, on Windows you write absolute path names like this:

C:\Desktop\cmpt166\a1.txt
C:\Desktop\cmpt166\ball.gif
C:\Desktop\cmpt166\data\ball.gif
C:\Desktop\macm101\a1.txt

In Windows, the \ character, called backward slash (or backslash), is used to separate the names. The C: at the start is the name of the disk drive the file is on.

On Mac and Linux computers, absolute path names are written a little differently:

/Desktop/cmpt166/a1.txt
/Desktop/cmpt166/ball.gif
/Desktop/macm101/a1.txt

There’s no C: at the start, and the forward slash character, /, is used to separate the names.

Notice that two different files have the same name, a1.txt. That’s okay because they are in different folders, and so have different absolute names. It would not be okay is if the two files were in the same folder: then there would be no way to tell them apart.

Relative Path Names

While absolute path names work well, they can be very long. Plus, there are often cases where we don’t know the names of all the containing folders. For example, if you use absolute path names in an assignment and then send that assignment to a TA for marking, your program will most likely not work correctly because the TA likely won’t have the same folder structure as you.

So to deal with these problems, we use relative path names. For relative path names to work, we need a current working folder that specifies the name of the folder that we (or a program) are currently using. For instance, suppose we agree that the folder cmpt166 is the current working folder. Then we can specify the files in it like this:

a1.txt
ball.gif

We don’t need any folder names at all since all the files we are interested in are inside the current working folder. Not only does this result in shorter names, but it will work in the case where you send your program to a TA since the names of all the containing folders are not specified.

The Processing Folder Structure

Lets take a moment to see how Processing arranges the files and folders for the programs you write in it. Suppose you write a program in the Processing editor, and save it with the name soccerBall. Then, inside the current sketchbook, Processing creates a folder called soccerBall, and inside that folder is a file called soccerBall.pde that contains the source code of your program.

Example Processing sketchbook for a program called soccerBall.

The folder that contains the .pde source file, in this case soccerBall, is the current working folder for the program.

Processing programs often have media files (e.g. image and sound files) associated with them. To keep things tidy, you should put all your media files in a folder called data that is in the same folder as the .pde source file. When you call a function such as loadImage("ball.gif"), Processing automatically searches the data folder for the file ball.gif.

Questions

  1. What is an absolute path name? What is a relative path name? How do they differ?

  2. What are two major problems with absolute path names?

  3. Draw a picture of a file system that has files and folders with the following names:

    /Desktop/courses/macm101/notes.txt
    /Desktop/courses/166/sketchbook/bounce/bounce.pde
    /Desktop/courses/166/sketchbook/jump/jump.pde
    /Desktop/courses/166/sketchbook/jump/data/jumper.pde