title: Easy way to replace last character in file?
body: <p>I simply want to replace the last character in my file. The reason is because when I write to my file, at the last point in which I write to the file, there is an extra <code>,</code> that is included at the end. I simply don't want to write that <code>,</code> at the end, but rather would want to replace it with a <code>]</code> if possible. Here is my attempt:</p>
  
  <pre><code>reader = csv.DictReader(open(restaurantsCsv), delimiter=';')
    with open(fileName, 'w+') as textFile:
      textFile.write('[')
      for row in reader:
        newRow = {}
        for key, value in row.items():
          if key == 'stars_count' or key == 'reviews_count':
            newRow[key] = float(value)
          else:
            newRow[key] = value
        textFile.write(json.dumps(newRow) + ',')
      textFile.seek(-1, os.SEEK_END)
      textFile.truncate()
      textFile.write(']')
  </code></pre>
  
  <p>It all works properly until I get to <code>textFile.seek(-1, os.SEEK_END)</code> where I want to seek the end of the file and I want to remove that last <code>,</code> in the file, but I get an error saying <code>io.UnsupportedOperation: can't do nonzero end-relative seeks</code>. Therefore, I made it so that my file opens with <code>wb+</code> parameters, but if I do that, then I can only write bytes to my file, and not strings. Is there any way I can simply replace the last character in my file with a <code>]</code> instead of a <code>,</code>? I know I can simply open the file to read, truncate the file, then open the file again to write the last <code>]</code> but that seems inefficient (as shown here):</p>
  
  <pre><code>   with open(filename, 'rb+') as filehandle:
       filehandle.seek(-1, os.SEEK_END)
       filehandle.truncate()
  
     with open(filename, 'a') as filehandle:
      filehandle.write(']')
  </code></pre>
  
  <p>Any help would be appreciated. Thanks!</p>