child_process spawnSync使用for循环迭代python stdout结果
child_process spawnSync iterate python stdout results using for loop
首先我将发布我的代码,
我将举一个我所面临的问题的例子:
for(var i=0;i<arrayleng.length;i++){
var oneScript = spawnSync('python',["/home/demo/mypython.py",arrayleng[i].path]);
//console.log(String(oneScript.stdout));
fs.readFile('/home/demo/' + arrayleng[i].filename + '.json','utf8',function(err,data){
if(err){
console.log(err);
}else{
console.log(data);
}
})
};
我想要刷子到同步,因为,我的python脚本将返回一些文件,它必须在每次执行python脚本后读取。但是现在它在完成python的执行后读取一次。并立即在控制台上打印出来。而不是每次执行python脚本后打印。
下面是一个nodejs示例:
var fs = require('fs')
var checkThese = ['/Users/jmunsch/Desktop/code_scraps/1.json', '/Users/jmunsch/Desktop/code_scraps/2.json']
checkThese.forEach(function(checkThisPath){
// Both of these will log first
console.log(`run first: ${checkThisPath}`)
var data = fs.readFileSync(checkThisPath, 'utf8')
var theJson = data
console.log(`run first: ${theJson}`)
// it might make sense that this would execute next
// but it doesn't
fs.readFile(checkThisPath, 'utf8', function(err, data){
// this callback executes later
console.log(`run second: ${checkThisPath}`)
console.log(`run second: ${data}`)
})
})
下面是一个python脚本和nodejs脚本的例子,以及它们是如何工作的。
python脚本listdir.py
:
import os
import sys
for x in os.listdir(sys.argv[1]):
print(x)
在上面的脚本中,我使用print
,它在每行之后附加一个'n
字符。如果您计划使用python的sys.stdout.write
将一个文件write
到stdout
,它将不会添加换行符。
nodejs代码:
var child_process = require('child_process');
var spawnSync = child_process.spawnSync
var checkThese = ['/Users', '/Users/jmunsch']
for (var i=0; i < checkThese.length;i++){
var checkThisPath = checkThese[i]
console.log(`Checking: ${checkThisPath}`)
var oneScript = spawnSync('python',["listdir.py", checkThisPath]);
oneScript.output.forEach(function(buffer){
if (buffer){
// convert to string
var x = buffer.toString('utf8')
console.log(`typeof x: ${typeof x}`)
// split by new line character
var y = x.split(''n')
console.log(`typeof y: ${typeof y}`)
// turn it into an array
var z = Array.prototype.slice.call(y)
// iterate each line in the array
z.forEach(function(pythonOutput){
console.log(`One python print(): ${pythonOutput}`)
})
}
})
}
nodejs代码的输出:
Checking: /Users
typeof x: string
typeof y: object
One python print(): .localized
One python print(): administrator
One python print(): casperadministrator
One python print(): jmunsch
One python print(): Shared
One python print():
typeof x: string
typeof y: object
One python print():
Checking: /Users/jmunsch
typeof x: string
typeof y: object
One python print(): .account
One python print(): .ansible
One python print(): .bash_sessions
One python print(): .cache
One python print(): .CFUserTextEncoding
One python print(): .config
One python print(): .cups
One python print(): .DS_Store
One python print(): .eclipse
One python print(): .gitconfig
One python print(): .ipython
One python print(): .lesshst
One python print(): .lldb
One python print(): .local
One python print(): .m2
One python print(): .netrc
One python print(): .node-gyp
One python print(): .node_repl_history
One python print(): .npm
One python print(): .nvm
One python print(): .oh-my-zsh
One python print(): .oracle_jre_usage
One python print(): .p2
One python print(): .profile
One python print(): .putty
One python print(): .python-eggs
One python print(): .rediscli_history
One python print(): .RSA
One python print(): .sh_history
One python print(): .ssh
One python print(): .swift
One python print(): .tooling
One python print(): .Trash
One python print(): .vagrant.d
One python print(): .viminfo
One python print(): .wget-hsts
One python print(): .zcompdump-LM-SFA-11003286-5.0.8
One python print(): .zsh-update
One python print(): .zsh_history
One python print(): .zshrc
One python print(): Applications
One python print(): Desktop
One python print(): Documents
One python print(): Downloads
One python print(): eclipse
One python print(): git
One python print(): Library
One python print(): Movies
One python print(): Music
One python print(): Pictures
One python print(): Public
One python print(): synced
One python print(): THEBIGPIN
One python print(): VirtualBox VMs
One python print():
typeof x: string
typeof y: object
One python print():
相关文章:
- 如何在javascript中迭代数字列表
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 如何迭代Array.prototype函数
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 在ejs-partial中对JSON对象进行迭代
- 如何在DataTables 2.1中迭代对象数组
- 使用递归属性迭代保留属性结构
- 正在停止.在jquery中的特定时间间隔内,每次迭代的每次执行
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- 主干-从模板中迭代的集合中获取特定的模型
- 创建一个方法,通过一个窗口进行迭代并获取Titanium中的所有控件
- 什么's是在javascript中迭代项的最佳方式
- 为什么这只是迭代 HTMLCollection 的奇怪元素
- 是否“;对于的“;循环迭代遵循JavaScript中的数组顺序
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- 在es6中,将带有回调的事件侦听器设置为可迭代的
- jQuery-迭代不正确?(太长,无法执行)
- 每个$.,循环获胜't逐个迭代HTML元素
- 在jQuery中用户交互之后逐个迭代python提供的数组
- child_process spawnSync使用for循环迭代python stdout结果