File Path

A file path is the text string that tells a computer how to locate a particular file or folder by naming each step through the directory hierarchy from some starting point. A path such as /home/ada/notes/todo.txt on Linux or C:\Users\Ada\Documents\todo.txt on Windows is an address rather than the file itself: the same file can be reachable by several different paths, and a path can point to something that no longer exists.

An absolute path starts at the top of the hierarchy and a relative path starts from wherever the program currently is. On Unix-like systems an absolute path begins with a single slash for the root, so /var/log/syslog means the same thing from anywhere, while a relative path such as data/train.csv is resolved against the process's current working directory and therefore means different files depending on where the program was launched. Two special names help: a single dot refers to the current folder and two dots to the parent, so ../models/best.pt climbs one level and descends into a sibling folder. Shells also expand a leading tilde to the user's home folder, but that is a convenience of the shell, not part of the path syntax the kernel understands.

Separators and roots differ between the two great path traditions. Unix uses the forward slash and one tree for all storage, with extra disks mounted at points inside it. Windows uses the backslash and gives each volume a drive letter, a convention inherited from MS-DOS and CP/M: A: and B: were reserved for floppy disks, which is why the first hard disk became C:. MS-DOS 2.0 introduced directories in 1983 when the slash was already in use for command options, so Microsoft chose the backslash for paths. Windows also has network paths in the UNC form \\server\share\file, and extended forms beginning with \\?\ that lift the traditional 260-character MAX_PATH limit. URLs, by contrast, always use forward slashes, so a web address and a local path can look alike without being interchangeable.

Program runs in /home/ada/project
this is the current working directory
It opens ../shared/data/train.csv
a relative path
Resolution, one component at a time
..
go up: /home/ada
shared
descend: /home/ada/shared
data
descend: /home/ada/shared/data
train.csv
final name: the file
Absolute result: /home/ada/shared/data/train.csv
launching from another folder would find a different file or none

Hard-coded paths are the commonest way a path breaks software. A script that contains C:\Users\Ada\data.csv works on exactly one machine, and one that depends on the current directory works only when started from the right place; this is behind a great many "file not found" errors in data science, where a notebook run from a different folder cannot see its dataset. Good practice is to build paths from a known anchor and join pieces with library tools rather than gluing strings: Python's os.path module and the pathlib module added in Python 3.4 (2014) handle separators correctly on any platform. Other frequent traps include spaces in names, which need quoting on the command line, and case: Linux treats Data and data as different paths, while default Windows and macOS file systems do not.

Paths are a security boundary as well as a convenience. In a path traversal attack, a program that builds a path from user input is fed a string containing many ../ components, escaping the intended folder to read or overwrite files such as password stores; the weakness has its own category, CWE-22, and is guarded against by normalizing the path and checking that the final result still lies inside the permitted directory. Symbolic links complicate the check, since a path inside the allowed folder can lead out of it, and the same class of bug appears when archives are extracted, in the well-known zip slip pattern, where entries carry crafted names.

A path is resolved by the file system and the operating system, and not everything that looks like a path is one. The kernel walks the components one by one, consulting each folder in turn and checking permissions along the way, so a path is only as stable as the folders it passes through; renaming any ancestor changes it. Modern systems also accept paths in forms that are not files at all, such as /dev/null, /proc/cpuinfo or a cloud storage address like s3://bucket/key, which uses a similar syntax but is resolved by a library calling a web service rather than by the local file system.

Read a file path as a set of directions rather than a destination: each separator is a step into a deeper folder, a leading root or drive letter fixes the starting point, and every bug in this area comes from forgetting whether the starting point was fixed or merely wherever the program happened to be standing.

Take file path further

Ask the copilot about this term, or have our engineers assess it against your process.