Skip to content

Create file if it doesn't exist, fail otherwise #12903

Answered by bartlomieju
retog asked this question in Q&A
Discussion options

You must be logged in to vote

@retog your solution is quite racy. You can do that using Deno.open() API and specifying relevant options:

import { writeAll } from "https://deno.land/std/streams/conversion.ts";
const file = await Deno.open(filename, { createNew: true, write: true });
await writeAll(file, content)
Deno.close(file.rid);

createNew option ensures that no file exists at specified location, otherwise it will throw an error.

See https://doc.deno.land/builtin/stable#Deno.OpenOptions for more details.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@retog
Comment options

@bartlomieju
Comment options

Answer selected by retog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants