Write File Safely
NodeJS
Hard
3 views
Problem Description
Write 'meetcode' to a file path, creating folders if needed.
Input Format
stdin: one output file path.
Output Format
Print DONE.
Sample Test Case
Input:
./out/meetcode.txt
Constraints
Use fs.mkdirSync with recursive true.
Official Solution
const fs = require('fs');
const path = require('path');
const outPath = fs.readFileSync(0, 'utf8').trim();
if (!outPath) process.exit(0);
const dir = path.dirname(outPath);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(outPath, 'meetcode', 'utf8');
console.log('DONE');
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!