Case:
In a project, the acquisition of data from the sensor, every 1G data collected after the data analysis, and ultimately only the results of data analysis, the data collected in memory, will consume a lot of memory, we want to put this data in a temporary file
Temporary files cannot be named and automatically deleted after closing
How to do?
Import Temporaryfile, namedtemporaryfile under Tempfile, which can set whether temporary files are permanently saved
#!/usr/bin/python3from tempfile Import temporaryfilefrom tempfile import namedtemporaryfile# When closing the file remove F = temporaryfile ( # Delete Default Delete, true to close temporary files without deleting, f_2 = Namedtemporaryfile (delete=false) f.write (b ' ABCD ' *100) f_2.write (b ' ABCD ' *100) # And can not be named by themselves. System assigns name, can only write bytes type print (F_2.name, f.name)
Python_ How to use temporary files