How can I test for the existence of a file or directory in q?

Short answer: key file_handle or key dir_handle

File handle example:

q)\touch /tmp/test.txt
q)key hsym ` $ “/tmp/test.txt”
`:/tmp/test.txt
q)

The function key returns an empty list when the file does not exist:

q)hdel `:/tmp/test.txt
`:/tmp/test.txt
q)() ~ key `:/tmp/test.txt
1b
q)

key is one of the most heavily overloaded functions in q. It accepts an argument of type dictionary, keyed table, table column, list, and integer. It’s easy to confuse it with keys, which turns out to be an entirely different function.

See also: ~ (match), \ (system), hdel, hsym

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


This work is licensed under a Creative Commons License.
The views and opinions expressed herein are those of the authors and do not necessarily reflect those of any other person or legal entity.
Kdb+ is the registered trademark of Kx Systems, Inc.