在下面的程序如何在python中将输出从第一个进程(函数)传递到下一个进程(函数)?
1)欲输出写入到一个文件 2)而且,也想通过管道的文件的最终输出,而不是从读取下游访问输出文件中的另一过程它。
用下面的Python代码:
global product
product = ""
product_file = open('product.txt', 'w')
def read_file():
file1 = open ('file01.txt', 'r')
data1 = read.file1().rstrip('\n')
data1_lines = data1.split('\n)
for lines in data1_lines:
columns = lines.split('\t')
x = int(columns[0])
y = int(columns[1])
prod = x*y
output = open('product.txt', 'a')
output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
output.close()
product += str(x +'\t'+ y +'\t'+ prod +'\n')
def manipulate_file():
global product;
for lines in product:
line = product.split('\t')
do..something.....
我想从def read_file()
访问最终输出文件,以在下游处理(函数,即def mainpulate_file()
),而不是再次打开它来使用。
我想要使用subprocess.call
和/或stdin-out
和/或tempfile
,并最终在完成时清除内存。
我读了几个例子,但没有找到任何明确的解决过程。
我将不胜感激任何解释。
我刚更新了这个问题。另外,你能告诉我你将如何使用'stdout'来在next/downstream函数中使用它。 – everestial007
@ everestial007如果您想在脚本的后面使用字符串,stdout根本没有任何帮助。我更新了我的答案,以显示您可以保留您生成的数据。查看[这个问题](http://stackoverflow.com/q/3385201/2211273)了解stdout。 – Kritzefitz
我想存储从函数#1生成的文件(信息),并将其作为下一个函数的“stdin”进行管理。 'print'将输出写入每个'for-loop'会话的屏幕。但是,我想将最终输出(而不是最后一行)输出到下一个函数。我想我不了解你的答案。但是,谢谢! – everestial007