title: (Python) Getting maximum recursion error, should I try another method or override it in this case?
body: <p>I'm writing files from a socket using recursion and I can see why I'm getting a maximum recursion error. My question is, should I ignore it?</p>
  <p>Here is the desired behavior:</p>
  <ul>
  <li>For the first data chunk, overwrite or create a file open in &quot;wb&quot; mode. For every remaining data chunk, append. If I don't do this, it will append data to an existing file if in &quot;ab&quot; mode, or it will only write each chunk to its own file in &quot;wb&quot; mode.</li>
  </ul>
  <p>Here is my solution:</p>
  <pre><code>def write_file(path:str, client_socket:socket, recv_len: int = 1, open_mode: str = &quot;wb&quot;):
      if recv_len:
          data = client_socket.recv(BUFFER_LEN)
          recv_len = len(data)
  
          with open(path, open_mode) as f:
              f.write(data)
  
          if recv_len &lt; BUFFER_LEN:
              client_socket.send(&quot;File successfully transfered.&quot;)
              return
          else:
              write_file(path, client_socket, recv_len, &quot;ab&quot;)
  </code></pre>
  <p>Here is my previous, less elegant solution that also works:</p>
  <pre><code>def download(path, client_socket):
      while True:
              file_buffer = b&quot;&quot;
              recv_len = 1
                      
              
              try:
                  while recv_len:
                      data = client_socket.recv(BUFFER_LEN)
                      recv_len = len(data)
  
                      # Overwrite file if exists.
                      with open(path, 'wb') as f:
                          f.write(data)
  
                      if recv_len &lt; BUFFER_LEN:
                          break
  
                      else:
                          while True:
                              data = client_socket.recv(BUFFER_LEN)
                              recv_len = len(data)
  
                              # Append rest of data chunks
                              with open(path, 'ab') as f:
                                  f.write(data)
  
                              if recv_len &lt; BUFFER_LEN:
                                  break
                      break
  
                  if not data:
                      break
  
                  client_socket.send(b&quot;Successfully saved file.&quot;)
  
              except:
                  client_socket.send(b&quot;File transfer failed.&quot;)
  </code></pre>
  <p>My recursive solution works for files up to around 80GB if I add this line:</p>
  <pre><code>sys.setrecursionlimit(20000)
  </code></pre>
  <p>Something tells me I'm gonna hear a lot of &quot;Well that limit is there for a good reason&quot;, which I don't doubt. But since my looped solution feels sloppy and wacky, and my recursive one goes into this huge recursive adventure - what would be the better way to move forward? Is there another alternative I'm not seeing?</p>
  <p>Edit: I omitted the Try, Except from the recursive model just to see if it would work. I plan on putting them back in.</p>
  <p>Edit: I am also not closing sockets after the transfer, making a <code>not data</code> state not something I can utilize.</p>