blob: c9de4af0b0f9ef8554a1822784c0fb669d0d0a65 [file] [log] [blame]
---
title: 'File'
---
This object is created during the analysis phase to represent a file or directory that will be read or written during the execution phase. It is not an open file handle, and cannot be used to directly read or write file contents. Rather, you use it to construct the action graph in a rule implementation function by passing it to action-creating functions. See the [Rules page](https://bazel.build/versions/8.2.1/extending/rules#files) for more information.
When a `File` is passed to an [`Args`](../builtins/Args) object without using a `map_each` function, it is converted to a string by taking the value of its `path` field.
## Members
* [basename](#basename)
* [dirname](#dirname)
* [extension](#extension)
* [is\_directory](#is_directory)
* [is\_source](#is_source)
* [is\_symlink](#is_symlink)
* [owner](#owner)
* [path](#path)
* [root](#root)
* [short\_path](#short_path)
* [tree\_relative\_path](#tree_relative_path)
## basename
```
string File.basename
```
The base name of this file. This is the name of the file inside the directory.
## dirname
```
string File.dirname
```
The name of the directory containing this file. It's taken from [path](#path) and is always relative to the execution directory.
## extension
```
string File.extension
```
The file extension of this file, following (not including) the rightmost period. Empty string if the file's basename includes no periods.
## is\_directory
```
bool File.is_directory
```
Returns true if this is a directory. This reflects the type the file was declared as (i.e. ctx.actions.declare\_directory), not its type on the filesystem, which might differ.
## is\_source
```
bool File.is_source
```
Returns true if this is a source file, i.e. it is not generated.
## is\_symlink
```
bool File.is_symlink
```
Returns true if this was declared as a symlink. This reflects the type the file was declared as (i.e. ctx.actions.declare\_symlink), not its type on the filesystem, which might differ.
## owner
```
Label File.owner
```
A label of a target that produces this File.
May return `None`.
## path
```
string File.path
```
The execution path of this file, relative to the workspace's execution directory. It consists of two parts, an optional first part called the *root* (see also the [root](../builtins/root) module), and the second part which is the `short_path`. The root may be empty, which it usually is for non-generated files. For generated files it usually contains a configuration-specific path fragment that encodes things like the target CPU architecture that was used while building said file. Use the `short_path` for the path under which the file is mapped if it's in the runfiles of a binary.
## root
```
root File.root
```
The root beneath which this file resides.
## short\_path
```
string File.short_path
```
The path of this file relative to its root. This excludes the aforementioned *root*, i.e. configuration-specific fragments of the path. This is also the path under which the file is mapped if it's in the runfiles of a binary.
## tree\_relative\_path
```
string File.tree_relative_path
```
The path of this file relative to the root of the ancestor's tree, if the ancestor's [is\_directory](#is_directory) field is true. `tree_relative_path` is only available for expanded files of a directory in an action command, i.e. [Args.add\_all()](../builtins/Args#add_all). For other types of files, it is an error to access this field.