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
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')
它是open()引发异常,但是...您如何定义result_summary?
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.
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
fp.readlines()
method returns a list of lines. Therefore you can't apply [-50:]
operator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.