使用python循环的每次迭代创建一个文本文件

时间:2019-07-01 14:36:27

标签: c python-3.7

我有一个运行C程序的python脚本:

cmd = ["/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta", "-w", "/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.txt", "-m", "5", "-e", "-0"]

    # Open/Create the output file
outFile = open("/Users/stordd/Desktop/StageI2M/Leiden/text_file/Output.txt", 'wb')

result = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out = result.stdout.read()

outFile.write(out)
outFile.close()

它需要一个文本文件作为输入并创建一个文本文件作为输出,我想创建一个循环,该循环将在每次迭代时创建一个文本文件而不替换上一个。我怎样才能做到这一点 ?

1 个答案:

答案 0 :(得分:1)

在打开的函数调用中,添加“ a”作为追加

outFile = open("/Users/stordd/Desktop/StageI2M/Leiden/text_file/Output.txt", 'ab')

https://docs.python.org/3/library/functions.html#open