One of the more important things when dealing with files is where they are located. Each file has an associated path which determines its location within your computer's file system. However, using a path as-is may make other machines not recognized the location of your file. In some cases, it can even cause the machine to fail to load the file at all. So, let's look at some representations of paths and how to use them within your own work.
Absolute and Relative Paths
Let's say we are trying to represent a file (example.txt
) within a file system:
For Unix-like machines (e.g., Mac, Linux), the file would be in:
/home
- user
- folder
- example.txt
For Windows machines:
C:
- Users
- user
- folder
- example.txt
So, how would we represent where this file is located? Well, one way would be to just list out the entire path to the file.
Unix-like:
/home/user/folder/example.txt
Windows:
C:\Users\user\folder\example.txt
This is known as the absolute path of the file. Absolute paths are useful if you want to represent a file directly on your file system without having to know any context about the execution environment. However, they are generally inoperable across different machines. In both paths, `user
` and `folder
` represent the user of the machine and the directory the file is in, respectively. These can differ across machines as not everyone has the same name or username, or the same organizational structure.
Now, let's say we have an execution shell (e.g., PowerShell, Terminal) which currently executes commands within `folder
`. In this case, we can represent our paths relative to the execution environment:
Unix-like:
./example.txt
Windows:
.\example.txt
This is known as the relative path of the file. In their full representation, relative paths start with a `.
` indicating the current execution environment. A relative path would allow you to access a file without knowing about the rest of the file structure, provided everything within the execution environment is static. However, you need to make sure the execution environment is consistent across runs:
If in `
folder
`:/home/user/folder/example.txt
If in `
user
`:/home/user/example.txt
(File doesn't exist!)
You should always use relative paths within your source code and specify where the code should be executed from.
Valid Path Characters
The names of each individual directory and file can also cause different operating systems to fail to do anything with the file. That's because different operating systems have different characters which are forbidden from being within a directory name or file name.
Let's list all the invalid characters:
Linux/Unix:
`
/
` (Forward Slash)0x00 (NULL Byte)
Windows:
`
<
` (Left Angle Bracket (Less Than))`
>
` (Right Angle Bracket (Greater Than))`
:
` (Colon (Works for NTFS Alternate Data Streams))`
"
` (Double Quote)`
/
` (Forward Slash)`
\
` (Backslash)`
|
` (Vertical Bar / Pipe)`
?
` (Question Mark)`
*
` (Asterisk)0x00 - 01F (ASCII Control Characters)
In addition, there are some restrictions on naming:
Windows:
Cannot be one of the following (regardless of file extension):
CON
PRN
AUX
NUL
COM1
-COM9
LPT1
-LPT9
Filenames cannot end with a `
.
` (dot).
Mac:
`
:
` (colons) are not permitted within Finder`
/
` (forward slashes) are not permitted within the Terminal
If you happen to transfer a file to an operating system that recognizes it as an invalid name, then your options of interacting with the file will likely be limited in some capacity (e.g. cannot read, open, write, delete the file without special operations).
My recommendation is to stick with alphanumeric characters (a-z), digits (0-9), underscores (_), hyphens (-), and dots (.) when naming files or directories. While other characters like spaces ( ) or exclamation marks (!) are allowed, some software readers may limit the format a filename should be in.
Now you have a better understanding of paths!
Some Additional Thoughts
Forward Slashes
When denoting paths in source code, I recommend using `/
` (forward slashes) between directories or filenames. Most software is interoperable and knows to transform the path separator to the one used by the file system. However, `\
` (backslashes) can be used as part of directory or file names on Unix-like systems. Almost all file systems respect `/
` as a path separator, hence my recommendation.
Hyphens and Underscores
Hyphens (-) and underscores (_) have some edge cases in some software programs. This is because package or module names typically do not allow hyphens, while some also disallow underscores. As such, I would limit package or module names to alphanumeric characters and digits.