API · Python

Python os.path.islink() Function

Learn how os.path.islink() identifies symbolic links, including links whose targets are missing.

On this page

os.path.islink() checks the path entry itself: it answers whether that entry is a symbolic link. The target may exist or be missing. This distinction is useful when a program needs to treat links differently from regular files or directories, or detect a dangling link before trying to use its target.

The function inspects the named filesystem path and reports whether that path is a symbolic link. It does not follow the link to decide whether the target exists. A link whose target has been removed is still a link, so it can be distinguished from a missing path that has no link entry.

This check is about the final path entry you pass. For example, if reports/current is a symbolic link to reports/2025, passing reports/current checks the link entry. Passing reports/2025 checks the target entry instead. Use the function when link identity matters, such as when a cleanup tool should unlink a link without treating its target as the item to remove.

Syntax and Parameters

Call the function as os.path.islink(path), where path is the filesystem path to inspect. The argument may be a string, bytes, or an object implementing the os.PathLike protocol. A pathlib.Path object is one common path-like value. The function does not create or modify the path.

Keep the path value in the form expected by the surrounding code. For instance, a Path object is convenient when using pathlib, while strings are common in code that uses os.path functions. Byte paths are useful in specialized filesystem code, but should be used consistently with other byte-path operations.

Return Value and Platform Support

The return value is a Boolean: True means the supplied path itself is a symbolic link; False means it is not identified as one. In particular, a missing ordinary path returns False, and a dangling symbolic link can return True because the link entry exists even though its destination does not.

Do not treat False as proof that a usable target exists. It may mean the path is an ordinary file or directory, a missing path without a link entry, or that the platform does not support symbolic links. On a platform without symbolic-link support, this function always returns False. If your next operation depends on the target being present, check that separately with an existence check appropriate to the operation.

Examples

The example below creates a regular file, a link to it, and a dangling link in a temporary directory. It uses only Python’s standard library and assumes the operating system and filesystem allow the process to create symbolic links.

import os
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as folder:
    root = Path(folder)
    target = root / "notes.txt"
    link = root / "notes-link.txt"
    dangling = root / "missing-link.txt"
    target.write_text("Notes", encoding="utf-8")
    link.symlink_to(target.name)
    dangling.symlink_to("not-created.txt")

    print(os.path.islink(link))
    print(os.path.islink(target))
    print(os.path.islink(dangling))
True
False
True

The first call checks the link entry, the second checks the ordinary target file, and the third checks a link whose destination is absent. On systems where symbolic-link creation is unavailable or restricted, the setup may fail; run the example only in an environment that permits creating links.

os.path.islink(link_path) and os.path.exists(link_path) answer different questions. The first asks whether the path entry is a symbolic link. The second asks whether the path resolves to an existing target. For a dangling link, the link check can be true while the existence check is false. For a regular file, the link check is false while the existence check is true.

If a workflow needs both facts, check them separately and name the results accordingly. For example, use a link check before unlinking an entry, and use an existence check before opening the destination. This avoids confusing the presence of a link with the availability of the resource it points to.