Using ‘F.namewithout.flush()or.close()maycauseanerrorbecausethefilemaynotexistwhenF.name' without '.flush()' or '.close()' may cause an error because the file may not exist when 'F.name’ is used. Use ‘.flush()’ or close the file before using ‘$F.name’.
Use tempfile.NamedTemporaryFile instead. From the official Python documentation: THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may refer to a file that did not exist at some point, but by the time you get around to creating it, someone else may have beaten you to the punch.