Phantomjs append to file with fs.write

2019-04-04 02:40发布

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);
}

标签: phantomjs
1条回答
可以哭但决不认输i
2楼-- · 2019-04-04 03:14

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.

查看更多
登录 后发表回答