0

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

logfile.write('Total Matched lines : {}'.format(len(matched_lines)))

or:

logfile.write('Total Matched lines : %d' % len(matched_lines))

Comments

0

You could say:

logfile.write('Total matches lines: ' + str(len(matched_lines)))

Or:

logfile.write('Total matches lines: {}'.format(len(matched_lines)))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.