首页 > 解决方案 > 尝试在正确的标题下按顺序将测试结果附加到文本文件中

问题描述

试图让这些测试系列将其结果添加到内容中,然后在脚本结束时将收集到的内容写入文本文件,并在标准规定的正确标题下。第一个测试正常工作,但之后没有一个。没有错误,只是没有附加信息。我需要弄清楚我做错了什么以及如何将内容附加在正确的部分下。请帮忙!

def writeReport(): #writes content to report file without truncating
   reportFile = 'report.txt'
   bigspacer = '================================================================================================================================'
   littlespacer = '--------------------------------------------------------------------------------------------------------------------------------'
   si = ' Single Install'
   criteria = ['0',
      '1 Did the installation complete successfully',
      '2 Installed shortcuts. *.ico files should get installed in bin folder to be used by registry entries and IQR/IQJ images %CADIQ_HOME%\\samples\\cadiq\\compare_*\\*.iqr',
      '3 Installed files/folders match those of the central area',
      '4 Was the license file correctly copied and updated (temp lic when only the viewer is installed (lic panel should not be displayed))',
      '5 Were correct CIQEL files in correct location (updated correctly)',
      '6 Were the correct cadscript/bin/plat-XXX/cadscript_XXX installed (no extras)',
      '7 Was cadiq or cadiq.bat updated correctly',
      '8 Were the correct version_xx.txt and cadscript.cfg files in their correct place',
      '9 Was cadscript.cfg updated correctly',
      '10 Was the correct pdelib files there',
      '11 Does a CADIQ Analysis work',
      '12 Overall Pass/Fail']
   installpath = drive + ':\\CADIQ\\v' + version + '_' + cadsys + '_si'
   passes = 0
   fails = 0
   NAs = 0

   content = bigspacer + '\n' + cadsys + si + '\n' + bigspacer + '\n'

# loop for each test criteria
   for test in criteria:
      if test == criteria[0]:
         continue

      content += '\n' + littlespacer + '\n' + test + '\n'
      testsuccess = 1
      nonapplicable = 0
      relevantfolder = None
  

# test 1
      if test == criteria[1]:         
         content += 'Things' + '\n'

     
# test 2 (Icons are on desktop, try looking here in test)
      if test == criteria[2]:
     content += 'Things' + '\n'     

# test 3
      if test == criteria[3]:
         content += 'Things' + '\n'

# test 4
      if test == criteria[4]:
         content += 'Things' + '\n'

# test 5
      if test == criteria[5]:
         content += 'Things' + '\n'

# test 6
      if test == criteria[6]:
         content += 'Things' + '\n'

# test 7
      if test == criteria[7]:
         content += 'Things' + '\n'

# test 8
      if test == criteria[8]:
         content += 'Things' + '\n'

# test 9
      if test == criteria[9]:
         content += 'Things' + '\n'

# test 10
      if test == criteria[10]:
         content += 'Things' + '\n'

# test 11
      if test == criteria[11]:
         content += 'Things' + '\n'

# test 12
      if test == criteria[12]:
         content += 'Things' + '\n'
# end loop

# write
   with open(reportFile, 'a') as f:
      f.write(content)

标签: pythonlisttestingtext

解决方案


这是因为每次您使用 python 向 .txt 文件写入内容时,整个文件都会被擦除,然后它会写入您输入的内容,所以当您第一次运行它时,文件会被写入第二次、第三次、第四次……时间文件的内容被取消并重写,因此您看不到任何更改,因此您应该尝试以下操作:

with open(reportFile, "r") as file:
    old = file.read()
#then 
with open(reportFile, 'a') as f:
    f.write(old + "\n" + content)

我敢肯定有更好的方法可以做到这一点,但这项工作也


推荐阅读