# File system

KDB-X communicates with the filesystem through

- one-shot operations
- handles to persistent connections

Handles are more efficient for multiple operations on a file.

> **Info — File paths are displayed separated with forward slashes, regardless of the operating system.**
>
>
## One-shot operations


[get set](get.md)       read/write or memory-map a data file¹
[value](value.md)         read a data file¹

[hcount](hcount.md)        file size
[hdel](hdel.md)          delete a file or folder
[hsym](hsym.md)          symbol/s to file symbol/s¹

[0: File Text](file-text.md)      read/write chars¹       [read0](read0.md)  read chars¹
[1: File Binary](file-binary.md)    read/write bytes¹       [read1](read1.md)  read bytes¹
[2: Dynamic Load](dynamic-load.md)   load shared object

[save](save.md#save)   [load](load.md)   a variable
[rsave](save.md#rsave)  [rload](load.md#rload)  a splayed table
[dsave](dsave.md)         tables
[?  Enum Extend](enum-extend.md#filepath)


¹ Has application beyond the file system.

### Setting and getting

Keywords [`set` and `get`](get.md) let you treat files as variables that persist in the filesystem.
```q
q)`:data/foo`:data/bar set'(42;"thin white duke")
`:data/foo`:data/bar
q)get `:data/foo
42
q)get `:data/bar
"thin white duke"
```

### File utilities


[hcount](hcount.md)        file size
[hdel](hdel.md)          delete a file or folder
[hsym](hsym.md)          symbol/s to file symbol/s


### Writing and reading

Any file can be read or written as bytes (binary).
Text-file primitives handle text files.

`0` associates with text; `1` with bytes.


[read0](read0.md)               [read1](read1.md)
[0: Load CSV](file-text.md#load-csv)         [1: Read Binary](file-binary.md#read-binary)
[0: Load Fixed](file-text.md#load-fixed)

[0: Save Text](file-text.md#save-text)        [1: Save Binary](file-binary.md#save-binary)


> **Tip — The [File Text operator `0:`](file-text.md) can also represent a table as strings, and interpret key-value pairs.**
>
>
### Tables


[save](save.md#save)   [load](load.md)   a table
[rsave](save.md#rsave)  [rload](load.md#rload)  a splayed table
[dsave](dsave.md)         tables
[?  Enum Extend](enum-extend.md#filepath)


KDB-X uses files and directories to persist database tables.
[Partitioning a table](../how_to/interact_with_databases/partition.md) divides its rows across multiple directories.
[Splaying a table](../how_to/interact_with_databases/splayed-tables.md) stores each column as a separate file.

## Connections

A persistent connection enables multiple operations on a file without repeatedly opening and closing it.

Opening a connection to a file returns a [handle](handles.md) to the connection. The handle takes the form of an int that is also an applicable value.

System handles 0, 1, and 2 are to the console, stdout, and stderr.
They are always open.


0 console          [hopen](hopen.md)   open a file¹
1 stdout           [hclose](hopen.md#hclose)  close a file¹
2 stderr


Opening a connection to a non-existent file creates it and any missing ancestor directories.

Applying the handle to data appends it to the file as bytes.
Applying the `neg` of the handle to char data appends it as text.
The result of a successful operation is the positive or negative handle.

### Text
```q
q)key `:foo/                            / does not exist
q)show h:hopen `:foo/bar.txt
12i
q)key `:foo/                            / file and dir created
,`bar.txt

q)neg[h] "hear the lark and hearken"
-12i
q)-12i "to the barking of the dog fox"
-12i
q)neg[h] "gone to ground"
-12i

q)hclose h
q)hcount `:foo/bar.txt
71
q)read0 `:foo/bar.txt
"hear the lark and hearken"
"to the barking of the dog fox"
"gone to ground"

q)read0 (`:foo/bar.txt;10;20)
"ark and hearken"
"to t"
```

### Bytes
```q
q)hopen ":foo/hello.dat"
7i
q)7i 0x68656c6c6f776f726c64
7i
q)hclose 7i
q)read1 `:foo/hello.dat
0x68656c6c6f776f726c64
```

## Relative filepaths

Relative filepaths are sought in the following locations, in order.

1. current directory
1. [`QHOME`](by-topic.md#environment)
1. [`QLIC`](by-topic.md#environment)

---

[File compression](../how_to/interact_with_databases/file-compression.md)


_Q for Mortals_
[§14 Introduction to KDB-X](../learn/q4m/14_Introduction_to_kdb+.md)
