## Paths & Directories

MCS 260 Fall 2021

Week 1 Discussion

### Files and Directories

A file is a named collection of data (e.g. a document). Files cannot contain other files.

A directory or folder is a named container for both files and directories.

### Full paths

Multiple files may share the same name. To specify a file, you also need to know what directory it is in.

A full path does this. It specifies the file name and all the directories containing 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 a separator (appears between directory names and file names)

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

"letter.pdf" is the filename

### The Desktop is just a directory

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

In Windows, the desktop directory for a user named "ddumas" is usually:


C:\Users\ddumas\Desktop


In OS X, it is usually:


/Users/ddumas/Desktop


In Linux, it is usually:


/home/ddumas/Desktop


### Example

This file on professor Dumas's desktop:

Has full path:


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


So one way to run it in Powershell would be:


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


### Current working directory

Terminal interfaces and some other programs have a notion of the current working directory.

There is a command to show the current directory in a terminal:


pwd


(print working directory)

Important: In a terminal, filenames without a full path are assumed to refer to files in the current directory.

### A common mistake

In Powershell, this command will probably fail:


python hello.py


It only looks in the current directory for a file named "hello.py". If a file with that name exists but is in a different directory, it will not be found.

To fix this, you have two choices:

• Replace "hello.py" with a full path
• Change the current working directory to the one that contains hello.py

### Changing the current working directory

Move to another directory given by a full path:


cd C:\Users\ddumas\Desktop


Move to another directory that is contained in the current one (a subdirectory):


cd Desktop


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


cd ..


### 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, first cd to Desktop, then run:


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


In PowerShell, with relative path:


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


### Where is my file?

If you have a file open in Visual Studio Code, you can ask for its full path to be copied to the clipboard.

Right-click on the file name tab and select "Copy path".