现在我使用:
pageHeadSectionFile = open('pagehead.section.htm','r')
output = pageHeadSectionFile.read()
pageHeadSectionFile.close()
Run Code Online (Sandbox Code Playgroud)
但为了使代码看起来更好,我可以这样做:
output = open('pagehead.section.htm','r').read()
Run Code Online (Sandbox Code Playgroud)
使用上述语法时,如何关闭文件以释放系统资源?
我无法弄清楚async/await是如何工作的.我稍微理解它,但我不能使它工作.
function loadMonoCounter() {
fs.readFileSync("monolitic.txt", "binary", async function(err, data) {
return await new Buffer( data);
});
}
module.exports.read = function() {
console.log(loadMonoCounter());
};
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用readFileSync,但是如果我这样做,我知道我永远不会理解async/await,我只会把问题埋没.
目标:调用loadMonoCounter()并返回文件的内容.
每次调用incrementMonoCounter()(每个页面加载)时,该文件都会递增.该文件包含二进制缓冲区的转储,并存储在SSD中.
不管我做什么,我在控制台中得到错误或未定义.
我有两种不同语言的文本文件,它们是逐行对齐的.即textfile1中的第一行对应于textfile2中的第一行,依此类推.
有没有办法同时逐行读取这两个文件?
下面是文件应该如何显示的示例,假设每个文件的行数大约为1,000,000.
textfile1:
This is a the first line in English
This is a the 2nd line in English
This is a the third line in English
Run Code Online (Sandbox Code Playgroud)
textfile2:
C'est la première ligne en Français
C'est la deuxième ligne en Français
C'est la troisième ligne en Français
Run Code Online (Sandbox Code Playgroud)
期望的输出
This is a the first line in English\tC'est la première ligne en Français
This is a the 2nd line in English\tC'est la deuxième ligne en Français
This is a the third line …Run Code Online (Sandbox Code Playgroud) 我试图在Node.js中了解同步与异步,特别是读取html文件.
在请求处理程序中,我正在使用的同步版本,其工作原理如下:
var fs = require("fs");
var filename = "./index.html";
var buf = fs.readFileSync(filename, "utf8");
function start(resp) {
resp.writeHead(200, {"Content-type":"text/html"});
resp.write(buf);
resp.end();
}
exports.start=start;
Run Code Online (Sandbox Code Playgroud)
编辑:我试图像这样重构代码:
var fs = require("fs");
var filename = "./index.html";
function start (resp) {
resp.writeHead(200, {"Content-Type":"text/html"});
fs.readFile(filename, "utf8", function (err, data) {
if (err) throw err;
resp.write(data);
});
resp.end();
}
Run Code Online (Sandbox Code Playgroud)
我得到一个空白页面,我想这是因为它应该等待所有数据都被读取,在resp.write(数据)之前,我该如何发出信号?
我正在尝试从base64中编码的客户端读取图像.如何用nodejs阅读?
我的代码:
// add to buffer base64 image
var encondedImage = new Buffer(image.name, 'base64');
fs.readFile(encondedImage, "base64", function(err, buffer){
if ( err ) {
console.log('In read file')
console.log(err)
} else {
// check err
lwip.open(buffer, 'jpg', function(err, image){
console.log('in open')
if ( err ) console.log(err)
if ( image ) console.log(image)
// check 'err'. use 'image'.
// image.resize(...), etc.
});
}
})
Run Code Online (Sandbox Code Playgroud)
但是,我收到了这个错误:
In read file
[Error: Path must be a string without null bytes.]
Run Code Online (Sandbox Code Playgroud)
*解决方案*
lwip 在open方法中接受缓冲区.
// add to buffer …Run Code Online (Sandbox Code Playgroud) 我在Haskell中编写了一堆代码来创建文本索引.top函数如下所示:
index :: String -> [(String, [Integer])]
index a = [...]
Run Code Online (Sandbox Code Playgroud)
现在我想给这个函数一个从文件读取的字符串:
index readFile "input.txt"
Run Code Online (Sandbox Code Playgroud)
哪个不起作用,因为readFile的类型为FilePath - > IO String.
无法将预期类型'String'与推断类型'IO String'匹配
我看到错误,但我找不到任何类型的函数:
IO String -> String
Run Code Online (Sandbox Code Playgroud)
我想成功的关键在于Monads的某些地方,但我找不到解决问题的方法.
我试过dtype用read_csvas 传递参数dtype={n: pandas.Categorical}但是这不能正常工作(结果是一个Object).手册不清楚.
我使用以下代码生成zip
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,但由于不明原因,直到我尝试才行
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
echo file_get_contents($zip_name);
Run Code Online (Sandbox Code Playgroud)
我很好奇在两种情况下都能找到正在发生的事情
我想在我的Xcode Swift Iphone项目中放一个.txt文件.首先,我只需将其从桌面拖放到项目的Supporting Files文件夹中即可.是不是像Android"资产"那样的文件夹,所以我可以将我的文件放在我想要的任何地方?
我的示例中的文件名为README.txt,它有一堆行和段落.
很简单,现在我想将README.txt文件的内容打印到视图中.
如果我的文件在项目/SupportFiles/README.txt中,我如何执行读取功能以及应插入的路径?
非常感谢!
我试图理解为什么下面的承诺设置不起作用.
(注意:我已经用async.map解决了这个问题.但是我想知道为什么我的下面的尝试不起作用.)
正确的行为应该是:bFunc应该运行尽可能多的时间来读取所有图像文件(bFunc下面运行两次),然后cFunc控制台打印"结束".
谢谢!
尝试1:它在cFunc()处运行并停止.
var fs = require('fs');
bFunc(0)
.then(function(){ cFunc() }) //cFunc() doesn't run
function bFunc(i){
return new Promise(function(resolve,reject){
var imgPath = __dirname + "/image1" + i + ".png";
fs.readFile(imgPath, function(err, imagebuffer){
if (err) throw err;
console.log(i)
if (i<1) {
i++;
return bFunc(i);
} else {
resolve();
};
});
})
}
function cFunc(){
console.log("End");
}
Run Code Online (Sandbox Code Playgroud)
尝试2:在这种情况下,我使用了for循环,但它执行顺序不正常.控制台打印:结束,bFunc完成,bFunc完成
var fs = require('fs');
bFunc()
.then(function(){ cFunc() })
function bFunc(){
return new Promise(function(resolve,reject){
function read(filepath) {
fs.readFile(filepath, function(err, imagebuffer){
if (err) throw …Run Code Online (Sandbox Code Playgroud)