Home > Back-end >  Create new file with other name if file name already exist in python
Create new file with other name if file name already exist in python

Time:02-23

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.

customername = "X"
workspacename = "Y"

structureDict = {
    "attribute": ["attributes/screens"],
    
    "content": ["containers", 
                "dataProcessing", 
                "fields", 
                "properties", 
                "sources", 
                "structures", 
                "usages"],
    
    "types": ["containers/types", 
              "dataProcessing/types", 
              "fields/types", 
              "properties/types", 
              "sources/types", 
              "structures/types", 
              "usages/types"]
}
 
for category in structureDict:
    for endpoint in structureDict[category]:
        print (category, endpoint)

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   "/"   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 ["attribute", "types"]:
            endpointFilename = endpoint.replace("/", "_")


        url = url_X   endpoint

        params = {"versionId":Workspace_id,
                "includeAccessData":"true", 
                  "includeAttributes":"true",
                  "includeLinks":"true"
                 }

        jsonResponse = requests.get(url, params=params, headers={"Authorization":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["page"] = str(i)
                    jsonResponse = requests.get(url=url, params = params, headers={"Authorization":accessToken}).json()['results']

                    writeContentToFile('a', customername, workspacename, category, endpointFilename, jsonResponse)
                    
        except:
            print(endpoint)
            next

CodePudding user response:

I don't think I understood your question well. but from what I got I can help you this much:
for checking if exists you can use os.path.exists(path_to_file) and putting it in a while loop would give you ability to check for existence of your file name and assign a new one (Y_2) if needed

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)
    
    c = 1
    while os.path.exists(path   "/"   (endpoint if c == 1 else endpoint   f'_{c}')   '.json'): c  = 1
    with open(path   "/"   (endpoint if c == 1 else endpoint   f'_{c}')   '.json', mode, encoding='utf-8') as f:
        json.dump(jsonContent, f, ensure_ascii=False, indent=4)

CodePudding user response:

You just need to make a small change in your writeContentToFile function

import os
from datetime import datetime


def writeContentToFile(mode, customername, workspacename, category, endpoint, jsonContent):
    path = os.path.join(os.getcwd(), customername, workspacename, category)
    date = datetime. now(). strftime("%Y_%m_%d") #getting the current date
    

    new_endpoint=endpoint[:] #Creating a new endpoint value
    index=2 #setting the counter to 2

    while os.path.exists(os.path.join(path, new_endpoint)): #keep on checking Y_DATE_2 , Y_DATE_3 until filename is unique
        new_endpoint=endpoint '_' date '_' index
        index =1

    
    Path(path).mkdir(parents=True, exist_ok=True)
    
    with open(path   "/"   new_endpoint   '.json', mode, encoding='utf-8') as f:
        json.dump(jsonContent, f, ensure_ascii=False, indent=4)


  
        f.close()

Explanation: What we are doing is that first we are getting the current date as you need, then we are creating a new variable new_endpoint and then we are checking if the current filename exists and until it exists we will keep on adding _1, _2 ,_3... to the filename until we have a a filename which is unique.

CodePudding user response:

You can try using logging within your code. This is built in with Python as you probably are already aware.

Import logging

Set the logging level to whichever you need (10 = Debug, 20 = Info, 30 = Warning, 40 = Error, 50 = Critical)

For what you need i recommend 20.

Depending on how in depth you want it to be, you can also pickle your files and add an if statement that checks whether the logging info you receive matches with an existing pickle file (people dont recommend using pickle because of security on the unpickling side of the operations).

You may also want to open you writing files specifying that they're not appendable.

  • Related