> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/syntax_and_semantics/literals/file.md).

# File

A File object represents the name of a local file:

```ruby
File("/path/to/file.ext")
```

Alternatively:

```ruby
%f(/path/to/file.ext)
%d(/path/to) + %f(file.ext)
```

A file can be opened for reading or writing, using the `open_r` and `open_w` methods, respectively:

```ruby
var file = File("file.txt")    # File object
var fh = file.open_r           # FileHandle object
say fh.lines                   # read the lines into an Array
```

Creating a new file:

```ruby
var fh = File("newfile.txt").open_w
fh.say("foo")   # write a newline to the file
fh.close        # close the filehandle
```

A File abstracts an entity on the filesystem by name; a FileHandle abstracts a file descriptor in memory.

```ruby
File("file.txt").read           # read the content of a file as a String (UTF-8)
File("file.txt").read(:raw)     # read the content of a file as a String (RAW)
```

See the [FileHandle](/sidef-lang/syntax_and_semantics/literals/filehandle.md) documentation for more information.
