Search HoldMyTwist

Browse all posts

Episode 41

The Wandering Picnic Map

There are two basket.txt's. One is a list of stuff for a picnic. The other is a list of tools.

Episodes

Computing Basics

    Robot is watching a monitor, and Duck is carrying a picnic basket and route card in the park with a tree and shed.
    Robot points at folder diagram in the monitor. Duck puts red flag in the folder icons.
    Duck is smiling at a brief route card and a red flag. The tree and the shed are the two locations shown on the display.
    Robot is carrying red flag, moves it from park to shed. Duck is inside the monitor.
    Duck is standing in the shed. He has the same card. The picnic basket is still in the park outside the shed door.
    Picnic basket is still below diagram.
    Duck is holding up a hammer, over an open box of tools. There's still the other picnic basket over on the right.
    Robot unrolls route from the root folder to the park to the picnic file.
    Duck pins path card to his hat and leaves the shed to enter the park. He leaves basket and toolbox where they are.
    Robot compares 2 snapshots, where absolute path is the same, but working directory is different. They both have the same full file address.
    Duck looks at a brief note inside the shed. You can see the picnic basket behind the door of the shed.
    The path trace shows the parent step prior to picnic file.
    Duck looks at the park map in the monitor. There's a shed, a pond and a picnic basket. Robot is watching from outside.
    There are four picture cards.
    Duck picks up the blue note, but the shed flag is still showing. Picnic basket and tools box remain.
    Robot points to flag on the shed still. Duck is having a picnic somewhere else in the park.

    Episode transcript

    The missing picnic

    Robot is watching a monitor, and Duck is carrying a picnic basket and route card in the park with a tree and shed.

    Diagram values: CWD /park; basket.txt; /park/basket.txt = PICNIC; /park/shed/basket.txt = TOOLS

    Duck: There are two basket.txt's. One is a list of stuff for a picnic. The other is a list of tools.

    Robot points at folder diagram in the monitor. Duck puts red flag in the folder icons.

    Diagram values: CWD /park; basket.txt -> /park/basket.txt

    Robot: A relative path begins at your current working directory.

    Duck is smiling at a brief route card and a red flag. The tree and the shed are the two locations shown on the display.

    Diagram values: basket.txt

    Duck: The short file name starts off in the current working directory.

    Robot is carrying red flag, moves it from park to shed. Duck is inside the monitor.

    Diagram values: NEXT CWD /park/shed; basket.txt

    Robot: What will be found on this card when we go into the shed?

    Start where you stand

    Duck is standing in the shed. He has the same card. The picnic basket is still in the park outside the shed door.

    Diagram values: CWD /park/shed

    Duck: No files have been moved.

    Picnic basket is still below diagram.

    Diagram values: basket.txt -> /park/shed/basket.txt; TOOLS

    Robot: relative basket.txt points to /park/shed/basket.txt

    Duck is holding up a hammer, over an open box of tools. There's still the other picnic basket over on the right.

    Diagram values: TOOLS

    Duck: Ah, that explains the hammer in my sandwich basket.

    Robot unrolls route from the root folder to the park to the picnic file.

    Diagram values: /park/basket.txt; ROOT /

    Robot: The leading / tells us that we should begin at the root.

    Move the starting point

    Duck pins path card to his hat and leaves the shed to enter the park. He leaves basket and toolbox where they are.

    Diagram values: /park/basket.txt

    Duck: I'm pinning the whole path to my hat.

    Robot compares 2 snapshots, where absolute path is the same, but working directory is different. They both have the same full file address.

    Diagram values: CWD /park/shed -> /park/basket.txt; CWD /park -> /park/basket.txt

    Robot: So the picnic file is still named by the absolute path.

    Duck looks at a brief note inside the shed. You can see the picnic basket behind the door of the shed.

    Diagram values: CWD /park/shed

    Duck: So is it possible to get to the picnic file with a shorter relative path name from the shed?

    The path trace shows the parent step prior to picnic file.

    Diagram values: .. -> /park; ../basket.txt -> /park/basket.txt

    Robot: Two dots - back to the parent directory. Then look for basket.txt.

    Find the pond note

    Duck looks at the park map in the monitor. There's a shed, a pond and a picnic basket. Robot is watching from outside.

    Diagram values: CWD /park/shed; QUERY ../pond/note.txt; /park HAS shed/ AND pond/; pond/ HAS note.txt

    Duck: We're in /park/shed. Where is ../pond/note.txt?

    There are four picture cards.

    Diagram values: 1 /park; 2 /park/pond; 3 /park/pond/note.txt

    Robot: First park then in pond and then note.txt.

    Duck picks up the blue note, but the shed flag is still showing. Picnic basket and tools box remain.

    Diagram values: ../pond/note.txt -> /park/pond/note.txt

    Duck: The note itself did not change folder. The relative path started from the specified working directory.

    Robot points to flag on the shed still. Duck is having a picnic somewhere else in the park.

    Diagram values: POSIX-STYLE; NO SYMBOLIC LINKS

    Robot: Duck finally found his picnic with the correct path.

    The lesson

    These are artificial, POSIX-like paths with a fixed directory structure. A path beginning with a slash is absolute, it starts at the root. A relative path is relative to the current working directory. A relative path of ".." refers to the parent directory. Changing the working directory, changes where relative paths start (but doesn't move anything). This example doesn't include symbolic links, mounts, renamed directories, or permissions checks.

    Try it

    Diagram values: CWD /park/pond; PATH ../shed/basket.txt

    Pick out the file you're choosing. Does the picnic file get moved?

    Answer

    /park/pond's parent is /park. The next path element is shed. basket.txt is the tools file. The path becomes /park/shed/basket.txt. /park/basket.txt (picnic file) is not moved.

    Sources

    Python documentation: os.path (opens in a new tab)