Appendix D: File systems

Published

2023-08-02

Introduction

When we write source code in Python (or any other language) our code is saved in a file or files. Oftentimes, we want to read data from a file or write data to a file. Accordingly, we must have some basic understanding of files and file systems.

While file systems will vary somewhat based on your operating system—macOS, Linux, or Windows—file systems share common features. First, they include files and directories (also called “folders”), second, they are organized hierarchically, and third, there is some physical device on which they are stored.

The physical device on which files and directories are stored is usually a hard disk drive (HDD) or solid state drive (SSD). Your laptop or desktop computer will have at least one of these.

Files and directories stored on such a device are arranged hierarchically in a tree-like structure. This means that directories can contain files or other directories. Each physical device will have a “root” directory, and may have multiple directories as branches. You may think of files as leaves on a branch. The location of each element in the file system is represented as a path going from the root to the element in question.

For example, the file on my laptop that contains the text you are reading right now has the path

/Users/clayton/Workspace/UVM/Instructor/cs1210/book/... ...appendix_d_file_systems/_file_systems.qmd

The root directory is signified by the first /. Subsequent slashes separate segments in the path. All the intermediate elements are directory names. The last element is the actual file: _file_systems.qmd.

This is to say that on my laptop’s SSD, there’s the root directory. The root directory contains a directory called Users. Within the Users directory, there’s another directory called clayton, within that, a directory called Workspace, and so on.

All modern operating systems provide a graphical user interface for browsing and interacting with the file system (for example, searching for files; creating, moving, deleting, or renaming files or directories; etc.), as well as a text-based command line interface.

The details of such a tree will vary from machine to machine and from operating system to operating system, but the basic idea is the same: there’s a hierarchy of directories, and directories contain other directories or files or both.

You may have noticed that the “tree” representing the file system is drawn upside down, with the root at the top. This is typical for all operating systems. Every element in your file system has a unique path, indicating how to get from the root to that element, be it a directory or file.

Filename extensions

Filenames usually include an extension which is used to indicate the type of file it is. For example, Python files have a .py extension, as in hello_world.py. Other file types make use of different extensions: .txt for plain-text files, .md for Markdown files, .csv for comma-separated value files, and so on.

It’s important to note that changing a filename extension doesn’t change the content of a file—you can’t convert Python source code to a PDF by changing the extension from .py to .pdf.

You should also be aware that operating systems (macOS, Linux, Windows) associate filename extensions with application software, and that operating systems will use this information to launch a particular application associated with a given filename extension when a file is double-clicked in the GUI file browser. For example, if you have a data file in CSV format with a .csv extension, double-clicking it will most likely launch Excel if you’re using Microsoft Windows, or Numbers if you’re using macOS. Sometimes this behavior is desirable, sometimes it is not. All operating systems allow you to change such associations if you wish.

The good, the bad, the ugly

One nice thing about modern operating systems is that they provide great search tools to find files in your file system. Just enter a keyword, or portion of a file name, and the file browser will show you your file. This is quite handy, and in many cases it eliminates the need to browse the file system to a particular location in search of a file.

One not-so-nice thing about modern operating systems is that they provide great search tools to find files in your file system. This means that casual users often have no idea where a file is actually located—what directory it’s contained in, or its full path. The convenience of search functionality can obscure (or make entirely invisible) the hierarchical structure of your file system.

Differences between operating systems

While all modern file systems share the general features outlined above, there are some minor differences. For example, macOS and Linux use “/” to separate path segments (in a command line interface), while Windows uses “\”. Windows assigns drive letters to disk drives, the default for the boot disk on a Windows machine being C. So on a Windows machine, the path to this file (given above), might be something like this instead:

C:\Users\clayton\Workspace\UVM\Instructor\cs1210\book\... ...appendix_d_file_systems\_file_systems.qmd

Different operating systems provide different GUIs, but they behave similarly. macOS has the Finder, Windows has its File Explorer, and most Linux distributions offer a choice of file browser: Nautilus, Dolphin, Thunar, Nemo, and Konqueror to name a few. Again, while these are somewhat different, they all provide similar interfaces to similar file systems.

You should familiarize yourself with the file browser for your OS. Make sure you can:

  • browse to any given location in your file system,
  • find a file without using the built-in search functionality,
  • rename a file,
  • delete a file,
  • create a new, empty folder.

Modern IDEs and browsing files

Many integrated development environments (IDEs) provide a built-in file browser that can be displayed as a pane within the IDE window. Some IDEs commonly used to write Python code include JetBrains’ PyCharm, Microsoft’s Visual Studio Code, the open source Spyder IDE, and Thonny. All have built-in file browsers.

File browser in Thonny

Some of these file browsers support drag-and-drop operations (for example, to move a file from one directory to another, to move a file from outside a project into a project, etc.). Others do not.

You should familiarize yourself with the file browser within the IDE of your choice. Make sure you can:

  • browse to a given file,
  • create a new Python file (with .py extension),
  • open a plain-text file (for example, either .txt or .csv file extension), and
  • rename, move, and delete files.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).