Paths & Directories

MCS 260 Fall 2020

Week 1 Discussion

Files and Directories

A file is a named object that stores data (e.g. a document). Files cannot contain other files.

A directory or folder is a container that stores files & directories.

Both files and folders are often represented by icons.

Full paths

A full path is a name that uniquely specifies a single file or directory by describing all of the nested directories that contain it.

Example (Windows):

C:\Users\sramanujan\Documents\letter.pdf

Example (Linux/OS X):

/Users/sramanujan/Documents/letter.pdf

Decoding a full path

Consider the Windows example:

C:\Users\sramanujan\Documents\letter.pdf

"C:" is the drive letter (specifies a storage device)

"\" is the path separator

"Users", "sramanujan", "Documents" are directories, each contained within the previous one

"letter.pdf" is the filename

The Desktop

Icons on the desktop are just files in a certain directory.

In Windows, the desktop for a user named USERNAME is usually:


            C:\Users\USERNAME\Desktop
        

In OS X, it is usually:


            /Users/USERNAME/Desktop
        

In Linux, it is usually:


            /home/USERNAME/Desktop
        

Example

This file on my desktop in Windows:

Has full path:


        C:\Users\ddumas\Desktop\hello.py
    

To run this Python script in Powershell I could use the command:


        python C:\Users\ddumas\Desktop\hello.py
    

Working directory

Graphical and terminal interfaces have a notion of the working directory.

To show the current working directory (PowerShell, OS X, or Linux):


        pwd
    

(print working directory)

If a filename is given without a full path, its full path is assumed to start with the working directory. This is called a relative path.

Moving around

Move to a directory described by its full path:


        cd C:\Users\ddumas\Desktop
    

Move to a subdirectory (a directory contained in the working directory):


        cd Desktop
    

Move to the parent directory, i.e. the one that contains the working directory:


        cd ..
    

"cd" works the same way in Windows, OS X, Linux

Running a script: three ways

We want to run hello.py, a script on the desktop.

In PowerShell, with absolute path:


PS C:\Users\ddumas> python C:\Users\ddumas\Desktop\hello.py
Hello world

In PowerShell, with relative path:


PS C:\Users\ddumas> python Desktop\hello.py
Hello world

In PowerShell, first cd to Desktop, then run:


PS C:\Users\ddumas> cd Desktop
PS C:\Users\ddumas\Desktop> python hello.py
Hello world