Delete File If Exists
NodeJS
Easy
4 views
Problem Description
Delete a file only if it exists, print OK.
Input Format
stdin: file path.
Constraints
Should not throw if missing.
Official Solution
const fs = require('fs');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);
try {
if (fs.existsSync(p)) fs.unlinkSync(p);
console.log('OK');
} catch (e) {
console.log('OK');
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!