My below code creates exception log files at location-

```
C:/Users/Desktop/SampleTestFiles/ProjectFiles/ExceptionLogFiles/

```

Initially code keeps writing into ExceptionLog\_1.txt file whenever exception occurs and when the size of file exceeds 1 MB it starts writing to ExceptionLog\_2.txt until its size is 1 MB. So far, it works perfect only for these 2 file creations and writing. When size of second file exceeds 1 MB it should log exceptions into a third log file ExceptionLog\_3.txt. But, it does not works. Code keeps on writing into second file.

How to modify my code to make sure a new file is created when size of latest log file exceeds 1 MB?

```
def WriteExceptionToFile(self, traceback):

    count = 1
    fileDir = 'C:/Users/Desktop/SampleTestFiles/ProjectFiles/ExceptionLogFiles/'
    # check if the path exists, create directory if not.
    if not (os.path.exists):
        os.mkdir(fileDir)

    filename = "ExceptionLog_"+ str(count) +".txt"
    filepath = os.path.join(fileDir, filename)

    try:
        if os.path.getsize(filepath) < 1048576: # if file size is less than 1 MB
            filename = "ExceptionLog_" + str(count) + ".txt"
        else:
            filename = "ExceptionLog_" + str(count + 1) + ".txt"        

    except OSError:
        Print("Path '%s' does not exists or is inaccessible" % filepath)
        filename = "ExceptionLog_1.txt"

    filepath = os.path.join(fileDir, filename)

    with open(filepath, 'a+') as f:
        traceback.print_exc(file=f)
        f.close()

```