Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantomjs append to file with fs.write

Tags:

phantomjs

How can I append to a file using fs.write()?

Using fs.write on the same files overwrites the content:

var fs = require('fs');
try {
    fs.write("file.txt", "Hello World", 'w');
    fs.write("file.txt", "Hello World", 'w');
} catch(e) {
    console.log(e);
}
like image 232
ThorSummoner Avatar asked Sep 02 '14 20:09

ThorSummoner


1 Answers

Use append mode a instead of [over]write mode w in the fs.write call.

var fs = require('fs');
try {
    fs.write("file.txt", "Hello World", 'a');
    fs.write("file.txt", "Hello World", 'a');
} catch(e) {
    console.log(e);
}

I inferred this based on the python open() C fopen documentation; Glad it worked, other file modes may work but were not tested by me.

like image 97
ThorSummoner Avatar answered Oct 24 '22 11:10

ThorSummoner