Python: Ending line every N characters when writing to text file
<p>I am reading the webpage at "<a href="https://google.com" rel="nofollow">https://google.com</a>" and writing as a string to a notepad file.  In the notepad file, I want to break and make a newline every N characters while writing, so that I don't have to scroll horizontally in notepad.  I have looked up a number of solutions but none of them do this so far.  Thanks for any suggestions.</p>

<pre><code>import urllib.request

page = urllib.request.urlopen("http://www.google.com")
webfile = page.readlines()

with open("file01.txt", 'w') as f:
   for line in webfile:
       f.write(str(line))

f.close()
</code></pre>

<p>This will split each line at 100 characters:</p>

<pre><code>for line in webfile:
    while len(line) &gt; 100:
        # split the line
        f.write(line[:100])  # write the first 100 characters
        f.write('\n')        # write the newline character
        line = line[100:]    # remember the remainder of the line
    f.write(str(line))
</code></pre>

python