2017-03-23 134 views
0

我想创建输出文件FileA和FileB,其中包含来自不同函数调用的输出。将“文件”作为参数传递给每个相应的函数并将其附加到它会更好吗?或者有没有其他智能的方法来处理它?谢谢!python函数输出写入文件

Output: 
FileA contains OutA1, OutA2 
FileB contains OutB1, OutB2 

(python2.7) 

def taskA1_func: 
    <code> 
    for lines in <code>: 
    yield OutA1 

def taskA2_func: 
    <code> 
    for lines in <code>: 
    yield OutA2 

def taskB1_func: 
    <code> 
    for lines in <code>: 
    yield OutB1 

def taskB2_func: 
    <code> 
    for lines in <code>: 
    yield OutB2 

write_to_file_func: 
    creates file FileA and FileB 

main() 

回答

0

对于Python 2.7(或3.1分别),你可以使用:

with open('FileA.txt', 'a') as FileA, open('FileB.txt', 'a') as FileB: 
    FileA.write('something') 
    FileB.write('something else') 
+1

检查你的答案 – abccd

+0

的感谢!修正了 – mmenschig

+1

还有问题... – abccd