如何用tempfile庫創(chuàng)建python進(jìn)程中的臨時(shí)文件
臨時(shí)文件在python項(xiàng)目中時(shí)常會(huì)被使用到,其作用在于隨機(jī)化的創(chuàng)建不重名的文件,路徑一般都是放在Linux系統(tǒng)下的/tmp目錄。如果項(xiàng)目中并不需要持久化的存儲(chǔ)一個(gè)文件,就可以采用臨時(shí)文件的形式進(jìn)行存儲(chǔ)和讀取,在使用之后可以自行決定是刪除還是保留。
tempfile庫的使用tempfile一般是python內(nèi)置的一個(gè)函數(shù)庫,不需要單獨(dú)安裝,這里我們直接介紹一下其常規(guī)使用方法:
# tempfile_test.pyimport tempfilefile = tempfile.NamedTemporaryFile()name = str(file.name)file.write(’This is the first tmp file!’.encode(’utf-8’))file.close()print (name)
上述代碼執(zhí)行的任務(wù)為:使用tempfile.NamedTemporaryFile創(chuàng)建一個(gè)臨時(shí)文件,其文件名采用的是隨機(jī)化的字符串格式,作為name這樣的一個(gè)屬性來調(diào)用。通過執(zhí)行這個(gè)任務(wù),我們可以查看一般是生成什么樣格式的臨時(shí)文件:
[dechin@dechin-manjaro tmp_file]$ python3 tempfile_test.py /tmp/tmppetcksa8[dechin@dechin-manjaro tmp_file]$ ll總用量 4-rw-r--r-- 1 dechin dechin 181 1月 27 21:39 tempfile_test.py[dechin@dechin-manjaro tmp_file]$ cat /tmp/tmppetcksa8cat: /tmp/tmppetcksa8: 沒有那個(gè)文件或目錄
在這個(gè)python代碼的執(zhí)行過程中,產(chǎn)生了tmppetcksa8這樣的一個(gè)文件,我們可以向這個(gè)文件中直接write一些字符串。這個(gè)臨時(shí)文件被存儲(chǔ)在tmp目錄下,與當(dāng)前的執(zhí)行路徑無關(guān)。同時(shí)執(zhí)行結(jié)束之后我們發(fā)現(xiàn),產(chǎn)生的這個(gè)臨時(shí)文件被刪除了,這是NamedTemporaryFile自帶的一個(gè)delete的屬性,默認(rèn)配置是關(guān)閉臨時(shí)文件后直接刪除。
持久化保存臨時(shí)文件需要持久化保存臨時(shí)文件是非常容易的,只需要將上述章節(jié)中的delete屬性設(shè)置為False即可:
# tempfile_test.pyimport tempfilefile = tempfile.NamedTemporaryFile(delete=False)name = str(file.name)file.write(’This is the first tmp file!’.encode(’utf-8’))file.close()print (name)
這里我們唯一的變動(dòng),只是在括號(hào)中加上了delete=True這一設(shè)定,這個(gè)設(shè)定可以允許我們持久化的存儲(chǔ)臨時(shí)文件:
[dechin@dechin-manjaro tmp_file]$ python3 tempfile_test.py /tmp/tmpwlt27ryk[dechin@dechin-manjaro tmp_file]$ cat /tmp/tmpwlt27rykThis is the first tmp file!設(shè)置臨時(shí)文件后綴
在有些場(chǎng)景下對(duì)于臨時(shí)文件的存儲(chǔ)有一定的格式要求,比如后綴等,這里我們將臨時(shí)文件的后綴設(shè)置為常用的txt格式,同樣的,只需要在NamedTemporaryFile的參數(shù)中進(jìn)行配置即可:
# tempfile_test.pyimport tempfilefile = tempfile.NamedTemporaryFile(delete=False, suffix=’.txt’)name = str(file.name)file.write(’This is the first tmp file!’.encode(’utf-8’))file.close()print (name)
由于還是設(shè)置了delete=True參數(shù),因此該臨時(shí)txt文件被持久化的保存在系統(tǒng)中的/tmp目錄下:
[dechin@dechin-manjaro tmp_file]$ python3 tempfile_test.py /tmp/tmpk0ct_kzs.txt[dechin@dechin-manjaro tmp_file]$ cat /tmp/tmpk0ct_kzs.txtThis is the first tmp file!總結(jié)概要
本文主要介紹了python中自帶的tempfile庫對(duì)臨時(shí)文件的操作,通過tempfile庫我們可以創(chuàng)建自動(dòng)刪除的或者持久化存儲(chǔ)的臨時(shí)文件,存儲(chǔ)路徑為L(zhǎng)inux系統(tǒng)下的/tmp目錄,而我們還可以根據(jù)不同的場(chǎng)景需要對(duì)產(chǎn)生的臨時(shí)文件的后綴進(jìn)行配置。
原文鏈接為:https://www.cnblogs.com/dechinphy/p/tempfile.html
以上就是如何用tempfile庫創(chuàng)建python進(jìn)程中的臨時(shí)文件的詳細(xì)內(nèi)容,更多關(guān)于tempfile庫創(chuàng)建臨時(shí)文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. XHTML 1.0:標(biāo)記新的開端2. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 匹配模式 - XSL教程 - 44. 秒殺場(chǎng)景的緩存、隊(duì)列、鎖使用Redis優(yōu)化設(shè)計(jì)方案5. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera6. ASP實(shí)現(xiàn)加法驗(yàn)證碼7. ASP基礎(chǔ)知識(shí)Command對(duì)象講解8. 輕松學(xué)習(xí)XML教程9. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟10. 讀大數(shù)據(jù)量的XML文件的讀取問題
