5.8 KiB
Executable File
More Python 101
Computer file systems and file paths
Computers store files on hard drives (among other things) - every device storing files typically has a filesystem, which are how the files are stored on the medium. There are a few different types, the differences between which should hopefully be transparent. The top level aspect of the filesystem is a heirarchy of folders in which files are stored. (There are also symlinks, hardlinks, and a few other constructs, but files and folders are what you should focus on at first.)
Typically there is a root folder. On Windows it would be something like C:\ or D:\ for a particular hard drive. On macs, your filesystem root is designated by a single backslash '/'
Your filesystem:
/ - your root folder
/Users - where your user folder goes
/jessicareynolds (or similar)
(various configuration files of yours)
/Desktop
/Downloads
/...
/Applications - where your mac GUI applications are (mostly installed)
... (other folders)
/Volumes - if you plug in external hard drives they probably show up here
/some_external_media
/opt - I think I saw that anaconda installed itself to /opt somewhere, but you probably won't need to know where.
/usr - directory containing various command line utilities that you can call in your terminal.
Each file has a location that can be specified by a string known as a filepath. There are both absolute and relative filepaths. Absolute file paths begin with a /, and are how you get from your file-system root to the file involved.
"/Users/jessicareynolds/Desktop/a_working_folder/subfolder/file.txt"
Relative file paths are how to get from your programs "current working directory" to the file. When running scripts from spyder, spyder has a particular directory it is currently working out of. It should be the same directory as the one listed in your file-tree tab. You can also run
os.getcwd()
in the command prompt to get a string that is the current filepath.
Suppose you are currently working from a_working_folder on your desktop. Your current working directory should be something like
"/Users/jessicareynolds/Desktop/a_working_folder" - or it may be shortened to
"~/Desktop/a_working_folder" - here ~ is shorthand for the user's folder. (I'm guessing from Linux, it may be different on macs.)
The relative path to file.txt would be "subfolder/file.txt", or maybe "./subfolder/file.txt"
Let's say your desktop has the following stuff on it
~/Desktop
/a_working_folder
your_python_script.py
/subfolder
file.txt
/subfolder_b
anotherfile.txt
/a_different_project_of_yours
a_third_file.csv
Current working directory: "~/Desktop/a_working_folder"
The path from there to file.txt would be "./subfolder/file.txt"
The path to anotherfile.txt would be "./subfolder_b/anotherfile.txt"
The path to a_third_file.csv would be "../a_different_project_of_yours/a_third_file.csv"
. - where you currently are
.. - one folder up from path
If you were loading these datafiles from your python script, you would have to use either a relative, or an absolute path pointing to the file from the current working directory used to run your script. While your current working directory is probably where you place your jupyter notebook, or your python file, it's not necessarily the case. (Check the file browser tab, or run os.getcwd() if you are not sure.)
Other useful commands for browsing filesystems
The filesystem tree and file-paths are a text representation of the structure of folders that your operating system gives you in your OS file browser.
You can navigate around, create folders and files, and move, copy and delete folders and files from the terminal.
Some terminal commands
ls - list contents of a directory
cd - change directory - you would call this
cd (somefilepath)
cd .. (move up one folder)
cd ./subfolder (move from ~/Desktop/a_working_folder to ~/Desktop/a_working_folder/subfolder
cd / - move to the root folder
mkdir - create a folder in your current working directory
rmdir - remove a folder in your current working directory
mv (source) (destination)
mv ./subfolder/* ./subfolder_b - take all files and folders in subfolder and move them over to subfolder_b
cp (source) (destination) - same as move, but you are copying files. Does not copy folders without a flag like -R (recursive)
Also, you can get help on how to use a terminal command using the man-pages command
man (command) brings up a help manual
pwd - how to get your present working directory from the terminal
Python Versions of this stuff (see filepathpractice.py)
Within a python script or notebook, there are various methods for maneuvering around filesystems. Most of these are within the os and os.path module
os.listdir() - lists the files and folders within a directory. It will return a list that can be iterated over with subsequent commands
os.getcwd() - get current working directory
os.path.abspath(rel_path_to_something) - returns the absolute path, given a relative path to a file or folder. The absolute path can sometimes be useful in unambiguously locating things.
os.path.isfile(pathstring) - is the thing at pathstring a file (or something else?)
os.path.isdir(pathstring) - is the thing at pathstring a folder (or something else?)
os.mkdir(path) - make a folder
you may have to wrap it in a try/catch block to avoid errors if the folder already exists
os.chdir(path) - change your current working directory from within a python script
Joining and splitting paths
(see the example file for the rest of this tutorial.)