1

I am using file_name.write to write multiple line considering spaces to write a file.

Code:

    file_handle.write('$TTL 1h\n')
    file_handle.write('@\tIN\tSOA\tns1.test.nimblestorage.com.\tis-ops.hpe.com. (\n'
                      )
    file_handle.write('\t\t\t%s\t; serial\n' % serial_number)
    file_handle.write('\t\t\t3h\t; refresh\n')
    file_handle.write('\t\t\t30m\t; retry\n')
    file_handle.write('\t\t\t30d\t; expire\n')
    file_handle.write('\t\t\t5m )\t; minimum\n')
    file_handle.write('\t\tNS\tns1.test.nimblestorage.com.\n')
    file_handle.write('\t\tNS\tns2.test.nimblestorage.com.\n')
    file_handle.write('\n')

But I need some multiline string code with all lines in a single file.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
Prabhu S
  • 15
  • 6

2 Answers2

1

Use triple quotes:

file_handle.write('''$TTL 1h
@\tIN\tSOA\tns1.test.nimblestorage.com.\tis-ops.hpe.com. (
\t\t\t{serial}\t; serial
...
'''.format(serial=serial_number))
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

There are reasons to want multi-line strings, in which case the answer by John Zwinck is the good one. However, if you want them for file I/O optimization:

Don't bother

Python already does the optimization for you: see the buffering option in https://docs.python.org/3/library/functions.html#open

Leporello
  • 638
  • 4
  • 12