【问题标题】:storing results into a file after multiple runs python多次运行python后将结果存储到文件中
【发布时间】:2014-03-31 08:18:02
【问题描述】:

我在 Python 中制作了一个可执行脚本,该脚本将文件名作为命令行参数。脚本运行并输出一个数字。我有几个必须运行脚本的文件,我想将所有结果存储在一个名为results.txt 的单独文件中。该文件应如下所示:

name_of_file_1 result_number_1
name_of_file_2 result_number_2
name_of_file_3 result_number_3
...
name_of_file_n result_number_n

这是我迄今为止尝试过的,但每次文件名和结果都会被覆盖。

args = sys.argv[1]
f = open('results.txt', 'w')
f.write(args + " " + str(len(i)) + "\n")
f.close()

我该怎么做?

【问题讨论】:

    标签: python file output executable


    【解决方案1】:

    您可以使用append 模式代替write 模式,如下所示:

    f = open('results.txt', 'a')
    f.write(args + " " + str(len(i)) + "\n")
    f.close()
    

    append 模式添加到现有文件,而write 模式只是清除文件或覆盖它。

    【讨论】:

      【解决方案2】:

      用途:

      f = open('results.txt', 'a')
      

      【讨论】:

        猜你喜欢
        • 2016-05-23
        • 1970-01-01
        • 2018-07-11
        • 1970-01-01
        • 2020-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        相关资源
        最近更新 更多