0

I am creating an email response to an overnight build, I want to get the last 50 lines from the results file and place it into a summary file. The code that I have done is below, can anyone help?

def email_success():

    fp = open(results_file, 'r')

    sum_file = (fp.readlines()[-50:])
    fp.close()

    myfile = open(result_summary,'w')

    myfile.write(sum_file)
    myfile.close()

I have got the error message below when trying this code:

Traceback (most recent call last):
  File "email_success.py", line 76, in <module>
    if __name__ == '__main__': myObject = email_success()
  File "email_success.py", line 45, in email_success
    myfile = open(result_summary,'w')
TypeError: coercing to Unicode: need string or buffer, tuple found

Thanks

The results summary is a variable that stores an address.

result_summary = (t, 'results_summary.txt')

Sorry made a stupid mistake, I forgot to add os.path.join

result_summary = os.path.join(t, 'results_summary.txt')

Thanks for the help

@alok It is a directory address, I forgot to add the os.join to make it one string. This is what was causing the error

chrisg
  • 40,337
  • 38
  • 86
  • 107

5 Answers5

5
TypeError: coercing to Unicode: need string or buffer, tuple found

Error says its expect string or buffer but you are passing tuple, so just join it with "" to make it to string

So, Try

sum_file = "".join(fp.readlines()[-50:])

UPDATE: because OP updated the question

if result_summary = (t, 'results_summary.txt')

Try

myfile = open(result_summary[1],'w')
YOU
  • 120,166
  • 34
  • 186
  • 219
2

It's open() raising the exception though... how did you define result_summary?

Andrew McGregor
  • 31,730
  • 2
  • 29
  • 28
2

result_summary is a tuple, it needs to be either a str or buffer. Your explanation has nothing to do with the error you posted.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
1
result_summary = (t, 'results_summary.txt')

and

 myfile = open(result_summary,'w')

means

 myfile = open((t, 'results_summary.txt'),'w')

which obviously won't work, try:

 myfile = open(result_summary[1],'w')

instead

Kimvais
  • 38,306
  • 16
  • 108
  • 142
0

fp.readlines() method returns a list of lines. Therefore you can't apply [-50:] operator.

Upul Bandara
  • 5,973
  • 4
  • 37
  • 60