Reputation: 25
I am trying to write a line to text file in python with below code but its giving me an error
logfile.write('Total Matched lines : ', len(matched_lines))
i know we cannot give 2 arguments for .write but whats the right way to do it. Any help is appreciated.
Upvotes: 0
Views: 708
Reputation: 481
You could say:
logfile.write('Total matches lines: ' + str(len(matched_lines)))
Or:
logfile.write('Total matches lines: {}'.format(len(matched_lines)))
Upvotes: 0
Reputation: 881113
Turn it into one argument, such as with the newish format strings:
logfile.write(f'Total Matched lines : {len(matched_lines)}\n')
Or, if you're running a version before where this facility was added (3.6, I think), one of the following:
logfile.write('Total Matched lines : {}\n'.format(len(matched_lines)))
logfile.write('Total Matched lines : ' + str(len(matched_lines)) + '\n')
Aside: I've added a newline since you're writing to a log file (most likely textual) and
write
does not do so. Feel free to ignore the\n
if you've made the decision to not write it on an individual line.
Upvotes: 2
Reputation: 1
Try:
logfile.write('Total Matched lines : {}'.format(len(matched_lines)))
or:
logfile.write('Total Matched lines : %d' % len(matched_lines))
Upvotes: 0