title: Python post a .xlsx file via Requests
body: <p>I'm trying to upload a xlsx file to a server using the following code and I'm getting a 409 error.
  I think its related to the fact there is an existing file on the server with the same name that I'm trying to post?  I basicly just want to overwrite the file if it exists?</p>
  <pre><code>import uuid
  import requests
  import io
  import json
  import os
  import time
  from datetime import datetime
  import configparser
  
  print(&quot;Employee Rate Files&quot;)
  config = configparser.ConfigParser()
  config.read('configUKG_EmployeeRate.ini')
  lastRunFilename = config.get('DEFAULT','lastRunFilename')
  host = config.get('DEFAULT','host')
  remoteLocation = config.get('DEFAULT','remoteLocation')
  path = config.get('DEFAULT','path')
  token = config.get('DEFAULT','token')
  print(&quot;(d_EmpRate) LastRunFileName = &quot; + lastRunFilename)
  print(&quot;(d_EmpRate) Path test = &quot; + path) 
  
  
  enconding_var = &quot;utf-8&quot;
  
  
  # Cut slashes at the end of paths if they are exist
  if path[-1] == '\\':
      path = path[:-1]
  if remoteLocation[-1] == '\/':
      remoteLocation = remoteLocation[:-1]
  
  #Function to compare stored and file last modified time
  def compareModificationTime(filePath):
      print(&quot;bbb &quot; + str(filePath in lastRunTime))
      print(&quot;filepath &quot; + filePath)
      print(&quot;last rt &quot; + str(lastRunTime))
      print(&quot;os get time &quot; + str(os.path.getmtime(filePath)))
      print(&quot;new dt &quot; + datetime.utcfromtimestamp(os.path.getmtime(filePath)).strftime('%c'))
      print(&quot;last rt int from file &quot; + str(lastRunTime.get(filePath)))
      if (filePath in lastRunTime and os.path.getmtime(filePath)&gt;int(lastRunTime.get(filePath))) or (filePath not in lastRunTime):
          print(&quot;(d_EmpRate) In compareModificationTime FileName true&quot; + filePath)
          return True
      else:
          print(&quot;(d_EmpRate) In compareModificationTime FileName false&quot; + filePath)
          os.remove(filePath)
          print(&quot;(d_EmpRate) Removing file: &quot; + filePath)
          return False
  
  # Function to upload file to Droppy via Sisense API
  def uploadFile (content, fileName, folderPath):
  
  
      if folderPath != path:
          remotePath = remoteLocation + folderPath.replace(path,'')
      else:
          remotePath = remoteLocation
  
      url = host +'/app/explore/!/upload?vId=0&amp;rename=0&amp;to=/'+remotePath
      boundary = str(uuid.uuid4()) 
      LF = &quot;\r\n&quot;;
      
      header = {'Authorization': token, 'Content-Type': 'multipart/form-data; boundary=\&quot;%s\&quot;'%boundary}
  
      bodyLines =[
       &quot;--%s&quot;%boundary,
       &quot;Content-Disposition: form-data; name=\&quot;filename\&quot;; filename=\&quot;%s\&quot;&quot;%fileName,
       &quot;Content-Type: application/octet-stream&quot;+LF,
       content,
       &quot;--%s-&quot;%boundary+LF 
      ] 
  
      bodyLines = [x + LF for x in bodyLines]
      bodyLines = ''.join(bodyLines)
      r = requests.post(url, data=bodyLines, headers=header)
      print(remotePath+'/'+fileName, r.status_code, r.reason)
      print(&quot;(d_EmpRate) File Uploaded!&quot;)
      print()
  
  # function to upload Files to Sisense via Droppy 11.1.0
  def uploadFileOld(filePath, fileName, folderPath):
  
      if folderPath != path:
          remotePath = remoteLocation + folderPath.replace(path,'')
      else:
          remotePath = remoteLocation
  
      url = host +'/app/explore/!/upload?vId=0&amp;rename=0&amp;to=/'+remotePath
  
      payload={}
  
      files = [('', (fileName, open(filePath, 'rb'), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))]
  
      headers = {
          'Authorization': token
      }
  
      r = requests.request(&quot;POST&quot;, url, headers=headers, data=payload, files=files)
  
      print(remotePath+'/'+fileName, r.status_code, r.reason)
      print(&quot;(d_EmpRate) XLS File Uploaded!&quot;)
      print()
  
  # function to upload Files to Sisense via File Browser 2.10.0
  def uploadFileNew(filePath, fileName, folderPath):
      print(&quot;in uploadnew&quot;)
      if folderPath != path:
          remotePath = remoteLocation + folderPath.replace(path,'')
          print(remotePath)
      else:
          remotePath = remoteLocation
          print(remotePath)
      url = host + '/app/explore/api/resources' + remotePath.replace('\\','/') +'/' + fileName
      print(url)
      payload=open(filePath, 'rb')
  
      xToken = getXtoken()
  
      headers = {
          'Authorization': token,
          'X-Auth': xToken
          }
  
      r = requests.request(&quot;POST&quot;, url, headers=headers, data=payload)
      
  
      print(&quot;end on uploadFileNew &quot; + remotePath+'/'+fileName, r.status_code, r.reason)
      print(&quot;(d_EmpRate) File Uploaded!&quot;)
      print()
  
  # function to get auth tocken for File Browser 2.10.0
  def getXtoken ():
      url = host + &quot;/app/explore/api/login&quot;
  
      payload={}
      headers = {
      'Authorization': token
      }
  
      response = requests.request(&quot;POST&quot;, url, headers=headers, data=payload)
  
      return response.text
  
  
  
  try:
      f = open(lastRunFilename, &quot;r&quot;)
      timestamp = f.read()
      lastRunTime = json.loads(timestamp)
  except:
      lastRunTime = {}
      print(&quot;(d_EmpRate) In except uploadFilec {} lastruntine &quot;.format(lastRunTime))
  
  
          
  if ( os.path.isdir(path)):
      print (&quot;in if&quot;)
      for root, subdirs, files in os.walk(path):
          print (&quot;in for 1&quot;)
          for entry in os.scandir(root):
              print (&quot;in for 2&quot;)
              print (&quot;in for 2 b &quot; + str(entry.is_file()))
              print (&quot;in for 2 b2 &quot; + str(entry.is_file()))
              if entry.is_file() and compareModificationTime(entry.path):
                  print (&quot;Path *** &quot; + entry.path)
                  print (&quot;Name &quot; + entry.name)                
                  uploadFileNew (entry.path, entry.name, root)
                  lastRunTime[entry.path] = int(time.time())
                  time.sleep(1)
  elif ( os.path.isfile(path) and compareModificationTime(path)):
      f = io.open(path, mode=&quot;r&quot;, encoding=&quot;utf-8&quot;)
      file = f.read()
  
      uploadFileNew (file, os.path.basename(path))
      lastRunTime[entry.path] = int(time.time())
      time.sleep(1)
      
  elif not os.path.exists(path):
      print (&quot;Specified path doesn't exist&quot;)
  
  
  with open(lastRunFilename, 'w') as f:
      json.dump(lastRunTime, f)
  
  if not files:
      print(&quot;(d_EmpRate) No files in Dir.&quot;)
      #service_func()
  else:
      print(&quot;(d_EmpRate) Directory not empty.&quot;)
      #service_func() 
  </code></pre>
  <p>If I delete the file on the server then the next time this script runs it does post the file just fine?
  Thanks for any help</p>