繁体   English   中英

Python以错误的顺序覆盖文件

[英]Python overwrite to file in wrong order

当我使用./program.py> temp.out运行程序时

我先获取所有单元测试输出,然后再将输出放入python中。 无论如何,我可以像在屏幕上那样将其显示在此文件中吗?

Test Results Suite "curl"
                               Name:     Checks  Failures   Time (s)
                        couch check:          -         -   Disabled
                   couch check fail:          -         -   Disabled
                database check fail:          -         -   Disabled
                    create database:          -         -   Disabled
                     database check:          -         -   Disabled
                  upload design doc:          -         -   Disabled
                     remove_witness:          -         -   Disabled
====================================================================
                              Total:          0         0

Passed
+=================================================================+
| Running: hba_test                                               |
| Skipping:abort/"Basic Sanity" delayedabort/"Abort Control List" |
+=================================================================+
+====================+
| Skipping: sdt_test |
+====================+
+======================+
| Skipping: dtd_tester |
+======================+
+===============+
| Running: pssm |
+===============+
+==============+
| Running: psm |
+==============+

这是执行每个单元测试并在其周围打印单独标题的代码

#calculate lengths to make sure header is correct length
        l1 = len(x)
        l2 = len(y)
        #if entire test suite is to be disabled
        if disable:
            headerBreak ="+" + "="*(l1+12) + "+"
            print headerBreak
            print "| Skipping: %s |" % x
        #if the test suite will be executed
        else:
            headerBreak =  "+" + "="*(max(l1,l2)+11) + "+"
            print headerBreak
            print "| Running: %s" % x, ' '*(l2-l1)+ '|'
            #if some suites are disabled but some are still running
            if 'disable=' in test:
               print "| Skipping:%s |" % y 
        print headerBreak
        #bitshift right to obtain correct return value, execution of each test.
        returnValue = os.system(path) >> 8
        #running total of failures in the program.
        failures += returnValue

运行该方法的最后一部分代码

#execute tests failures = execTests(path, testList)
#exit program with returncode as number of failures sys.exit(failures)

它看起来应该像这样:

+==============+
| Running: ssm |
+==============+

Test Results Suite "Secondary Set Manager Tests"
                               Name:     Checks  Failures   Time (s)
              SSM_1 validate checks:         30         0      0.002
                 SSM_2 group create:          6         0      0.001
           SSM_3 rcvd invalid group:          3         0      0.001
            SSM_4 rcvd invalid data:          9         0      0.001
               SSM_5 aborted subset:          7         0      0.000
          SSM_6 pri node down abort:         14         0      0.000
        SSM_7 excess ios in subsets:          6         0      0.000
              SSM_8 all ss received:         11         0      0.000
                  SSM_9 applying ss:         12         0      0.000
               SSM_10 applying ss 2:         18         0      0.000
            SSM_11 subsets complete:         32         0      0.001
     SSM_12 subsets complete errors:         19         0      0.000
           SSM_13 apply waiting set:         40         0      0.000
                 SSM_14 extend test:         14         0      0.000
               SSM_15 group destroy:          6         0      0.000
                 SSM_16 null params:          2         0      0.001
                  SSM_17 stop group:         26         0      0.001
                SSM_18 dupe receive:          6         0      0.000
           SSM_19 apply waiting set:         36         0      0.001
====================================================================
                              Total:        297         0

Test Results Suite "Secondary Subset Manager Tests"
                               Name:     Checks  Failures   Time (s)
             SSSM_1 Validate Checks:         14         0      0.001
                SSSM_2 Steady State:         92        42      0.001
              SSSM_3 Multi Sequence:        417       227      0.003
                  SSSM_4 Test Abort:         69         6      0.001
      SSSM_5 Test Inconsistent Mreq:         10         0      0.001
                 SSSM_6 Test extend:         37         1      0.001
          SSSM_7 Test unexpected IO:         11         0      0.001
            SSSM_8 test null params:          5         0      0.000
                  SSSM_9 exceptions:          0         1      0.001
            SSSM_10 done_incomplete:         20         0      0.001
                  SSSM_11 failed io:         92        42      0.001
====================================================================
                              Total:        767       319
Failed Cases [ssm]:
   Secondary Subset Manager Tests/"SSSM_2 Steady State"
   Secondary Subset Manager Tests/"SSSM_3 Multi Sequence"
   Secondary Subset Manager Tests/"SSSM_4 Test Abort"
   Secondary Subset Manager Tests/"SSSM_6 Test extend"
   Secondary Subset Manager Tests/"SSSM_9 exceptions"
   Secondary Subset Manager Tests/"SSSM_11 failed io"


Overall Failures: 319

当您从终端将输出重定向到文件时,缓冲模式将从行缓冲更改为使用固定大小的缓冲区。 这意味着换行不再触发刷新。

因此,您的print输出将被缓冲,但是使用os.system()运行的测试将在处理完成时刷新缓冲区。

解决方案是在运行os.system()之前,在print语句之后显式刷新:

import sys

# ....
print headerBreak
sys.stdout.flush()
returnValue = os.system(path) >> 8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM