Is there a method of creating a text file without opening a text file in "w" or "a" mode? For instance If I wanted to open a file in "r" mode but the file does not exist then when I catch IOError I want a new file to be created e.g.:
while flag == True:
try:
    # opening src in a+ mode will allow me to read and append to file
    with open("Class {0} data.txt".format(classNo),"r") as src:
        # list containing all data from file, one line is one item in list
        data = src.readlines()
        for ind,line in enumerate(data):
            if surname.lower() and firstName.lower() in line.lower():
                # overwrite the relevant item in data with the updated score
                data[ind] = "{0} {1}\n".format(line.rstrip(),score)
                rewrite = True
            else:
                with open("Class {0} data.txt".format(classNo),"a") as src: 
                    src.write("{0},{1} : {2}{3} ".format(surname, firstName, score,"\n"))
    if rewrite == True:
        # reopen src in write mode and overwrite all the records with the items in data
        with open("Class {} data.txt".format(classNo),"w") as src: 
            src.writelines(data)
    flag = False
except IOError:
    print("New data file created")
    # Here I want a new file to be created and assigned to the variable src so when the
    # while loop iterates for the second time the file should successfully open
 
     
     
     
    