title: Create new file with other name if file name already exist in python
body: <p>For the moment my script can create a file and the content. However, I would like to have a way in my code to directly noticed if the file already exists, and then if it exists, it creates a new file which will not be Y but Y_2 or Y with the current date. So I can have a history of my file created.</p>
  <pre><code>customername = &quot;X&quot;
  workspacename = &quot;Y&quot;
  </code></pre>
  <hr />
  <pre><code>structureDict = {
      &quot;attribute&quot;: [&quot;attributes/screens&quot;],
      
      &quot;content&quot;: [&quot;containers&quot;, 
                  &quot;dataProcessing&quot;, 
                  &quot;fields&quot;, 
                  &quot;properties&quot;, 
                  &quot;sources&quot;, 
                  &quot;structures&quot;, 
                  &quot;usages&quot;],
      
      &quot;types&quot;: [&quot;containers/types&quot;, 
                &quot;dataProcessing/types&quot;, 
                &quot;fields/types&quot;, 
                &quot;properties/types&quot;, 
                &quot;sources/types&quot;, 
                &quot;structures/types&quot;, 
                &quot;usages/types&quot;]
  }
   
  for category in structureDict:
      for endpoint in structureDict[category]:
          print (category, endpoint)
  </code></pre>
  <hr />
  <pre><code>def writeContentToFile(mode, customername, workspacename, category, endpoint, jsonContent):
      path = os.path.join(os.getcwd(), customername, workspacename, category)
      Path(path).mkdir(parents=True, exist_ok=True)
      
      with open(path + &quot;/&quot; + endpoint + '.json', mode, encoding='utf-8') as f:
          json.dump(jsonContent, f, ensure_ascii=False, indent=4)
  
  
    
          f.close()
  
  
  for category in structureDict:
      for endpoint in structureDict[category]:
          
          endpointFilename = endpoint
          
          if category in [&quot;attribute&quot;, &quot;types&quot;]:
              endpointFilename = endpoint.replace(&quot;/&quot;, &quot;_&quot;)
  
  
          url = url_X + endpoint
  
          params = {&quot;versionId&quot;:Workspace_id,
                  &quot;includeAccessData&quot;:&quot;true&quot;, 
                    &quot;includeAttributes&quot;:&quot;true&quot;,
                    &quot;includeLinks&quot;:&quot;true&quot;
                   }
  
          jsonResponse = requests.get(url, params=params, headers={&quot;Authorization&quot;:accessToken}).json()
  
          writeContentToFile('a', customername, workspacename, category, endpointFilename, jsonResponse)
          
          try:
              jsonResponsePages = jsonResponse['pages']
  
              if int(jsonResponsePages) != 1:
                  for i in range(2, jsonResponsePages+1, 1):
  
                      params[&quot;page&quot;] = str(i)
                      jsonResponse = requests.get(url=url, params = params, headers={&quot;Authorization&quot;:accessToken}).json()['results']
  
                      writeContentToFile('a', customername, workspacename, category, endpointFilename, jsonResponse)
                      
          except:
              print(endpoint)
              next
  </code></pre>