PyTorch filename print to csv file not defined

I am trying to print the file name into a csv file but getting an error message file is not defined:

        output = model(images) 
        preds = output.sum(dim=[1,2,3]) 
        with open('predsLilas1.csv','a') as fd:
            fd.write( ','.join(map(str, preds.detach().tolist())) + '\n')
            fd.write( ','.join(map(str, targets.detach().tolist())) + '\n')
            fd.write( ','.join(map(str, filename.detach().tolist())) + '\n')

Based on the error message I guess predsLilas1.csv is not created, while you are trying to append to the file. You could try to pass a+ as the mode, which should create the file, if it doesn’t exist or make sure that the file is in the right folder via os.path.exists('predsLilas1.csv').

1 Like