1

I am unable to write to a text file. My function looks like this. The file is created but after running the code the file remains empty. I am unable to figure out what the problem is.

def compare(prjCode, prjName, stCode, stName, dCode, dName, sdCode, sdName):
    with open('C:\\Users\\NCOG1\\Desktop\\test.txt', 'w') as f:
        percentMatch = 0
        sdLen = len(sdName)
        prjLen = len(prjName) 
        if (sdLen > prjLen): 
            if ((sdName.find(prjName) != -1)):
                res = sdName.index(prjName)
                percentMatch = (prjLen/sdLen)*100
                #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
                #print(prjCode + "," + prjName + "," + sdCode + "," + sdName + str(round(percentMatch)) + "," + stCode + "," + dCode + "," + sdCode)
                f.write(prjCode + "," + prjName + "," + sdCode + "," + sdName + "," + str(round(percentMatch)) + "," + stCode + "," + stName + "," + dCode + "," + dName + "%\n")
            else:
                res = 0
                #print(res)
        elif (prjLen >= sdLen):
            if ((prjName.find(sdName) != -1)):
                res = prjName.index(sdName)
                percentMatch = (sdLen/prjLen)*100
                #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
                #print(prjCode + "," + prjCode + "," + sdCode + "," + sdName + str(round(percentMatch) + "," stCode + "," + dCode + "," + sdCode)
                f.write(prjCode + "," + prjName + "," + sdCode + "," + sdName + "," + str(round(percentMatch)) + "," + stCode + "," + stName + "," + dCode + "," + dName + "%\n")                                                    
            else:
                res = 0
                #print(res)  
    f.close()

1 Answer 1

1

1.For to write something in file you need to convert those text or int to String which you didn't do that that's the main problem

2.The path which you types its not rendered because its not identified as a string for that you need to convert your path into raw string for that you need to append "r" before starting of the path which I mentioned in corrected code.

3.For example i call function compare() for dummy data, now check below code and try to run and let me know if you have any doubt.

def compare(prjCode, prjName, stCode, stName, dCode, dName, sdCode, sdName):
    with open(r'C:\\Users\\NCOG1\\Desktop\\test.txt', 'w') as f:
    percentMatch = 0
    sdLen = len(sdName)
    prjLen = len(prjName) 
    if (sdLen > prjLen): 
        if ((sdName.find(prjName) != -1)):
            res = sdName.index(prjName)
            percentMatch = (prjLen/sdLen)*100
            #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
            #print(prjCode + "," + prjName + "," + sdCode + "," + sdName + str(round(percentMatch)) + "," + stCode + "," + dCode + "," + sdCode)
            f.write(str(prjCode) + "," + prjName + "," + str(sdCode) + "," + sdName + "," + str(round(percentMatch)) + "," + str(stCode) + "," + stName + "," + str(dCode) + "," + str(dName) + "%\n")
        else:
            res = 0
            #print(res)
    elif (prjLen >= sdLen):
        if ((prjName.find(sdName) != -1)):
            res = prjName.index(sdName)
            percentMatch = (sdLen/prjLen)*100
            #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
            #print(prjCode + "," + prjCode + "," + sdCode + "," + sdName + str(round(percentMatch) + "," stCode + "," + dCode + "," + sdCode)
            f.write(str(prjCode) + "," + prjName + "," + str(sdCode) + "," + sdName + "," + str(round(percentMatch)) + "," + str(stCode) + "," + stName + "," + str(dCode) + "," + str(dName) + "%\n")                                                    
        else:
            res = 0
            #print(res)  


compare(1,"harsh",0,"fff",34,32,78,"hharshbhut")
Sign up to request clarification or add additional context in comments.

1 Comment

The file is created but nothing is written on it...with your example too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.