<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "System",
  "identifier" : "/documentation/System/FilePath",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "System"
    ],
    "preciseIdentifier" : "s:6System8FilePathV"
  },
  "title" : "FilePath"
}
-->

# FilePath

Represents a location in the file system.

```
struct FilePath
```

## Overview

This structure recognizes directory separators  (e.g. `/`), roots, and
requires that the content terminates in a NUL (`0x0`). Beyond that, it
does not give any meaning to the bytes that it contains. The file system
defines how the content is interpreted; for example, by its choice of string
encoding.

On construction, `FilePath` will normalize separators by removing
redundant intermediary separators and stripping any trailing separators.
On Windows, `FilePath` will also normalize forward slashes `/` into
backslashes `\`, as preferred by the platform.

The code below creates a file path from a string literal,
and then uses it to open and append to a log file:

```
let message: String = "This is a log message."
let path: FilePath = "/tmp/log"
let fd = try FileDescriptor.open(path, .writeOnly, options: .append)
try fd.closeAfter { try fd.writeAll(message.utf8) }
```

File paths conform to the
<doc://com.apple.documentation/documentation/Swift/Equatable>
and <doc://com.apple.documentation/documentation/Swift/Hashable> protocols
by performing the protocols’ operations on their raw byte contents.
This conformance allows file paths to be used,
for example, as keys in a dictionary.
However, the rules for path equivalence
are file-system–specific and have additional considerations
like case insensitivity, Unicode normalization, and symbolic links.

## Topics

### Creating a File Path

[`init()`](/documentation/System/FilePath/init())

Creates an empty, null-terminated path.

[`init(stringLiteral:)`](/documentation/System/FilePath/init(stringLiteral:))

Creates a file path from a string literal.

### Working with File Paths

[`length`](/documentation/System/FilePath/length)

The length of the file path, excluding the null terminator.

[`description`](/documentation/System/FilePath/description)

A textual representation of the file path.

[`debugDescription`](/documentation/System/FilePath/debugDescription)

A textual representation of the file path, suitable for debugging.

### Interacting with C APIs

[`withCString(_:)`](/documentation/System/FilePath/withCString(_:))

For backwards compatibility only. This function is equivalent to
the preferred `withPlatformString`.

### Comparing File Paths

### Encoding File Paths

## Relationships

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`ExpressibleByExtendedGraphemeClusterLiteral`](/documentation/Swift/ExpressibleByExtendedGraphemeClusterLiteral)

[`Hashable`](/documentation/Swift/Hashable)

[`Encodable`](/documentation/Swift/Encodable)

[`ExpressibleByUnicodeScalarLiteral`](/documentation/Swift/ExpressibleByUnicodeScalarLiteral)

[`ExpressibleByStringLiteral`](/documentation/Swift/ExpressibleByStringLiteral)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Escapable`](/documentation/Swift/Escapable)

[`Copyable`](/documentation/Swift/Copyable)

[`Decodable`](/documentation/Swift/Decodable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)