Python tempfile模塊學(xué)習(xí)筆記(臨時(shí)文件)
tempfile.TemporaryFile
如何你的應(yīng)用程序需要一個(gè)臨時(shí)文件來存儲數(shù)據(jù),但不需要同其他程序共享,那么用TemporaryFile函數(shù)創(chuàng)建臨時(shí)文件是最好的選擇。其他的應(yīng)用程序是無法找到或打開這個(gè)文件的,因?yàn)樗]有引用文件系統(tǒng)表。用這個(gè)函數(shù)創(chuàng)建的臨時(shí)文件,關(guān)閉后會(huì)自動(dòng)刪除。
實(shí)例一:
import os
import tempfile
print 'Building a file name yourself:'
filename = '/tmp/guess_my_name.%s.txt' % os.getpid()
temp = open(filename, 'w+b')
try:
print 'temp:', temp
print 'temp.name:', temp.name
finally:
temp.close()
os.remove(filename) # Clean up the temporary file yourself
print 'TemporaryFile:'
temp = tempfile.TemporaryFile()
try:
print 'temp:', temp
print 'temp.name:', temp.name
finally:
temp.close() # Automatically cleans up the file
這個(gè)例子說明了普通創(chuàng)建文件的方法與TemporaryFile()的不同之處,注意:用TemporaryFile()創(chuàng)建的文件沒有文件名
輸出:
$ python tempfile_TemporaryFile.py
Building a file name yourself:
temp: <open file '/tmp/guess_my_name.14932.txt', mode 'w+b' at 0x1004481e0>
temp.name: /tmp/guess_my_name.14932.txt
TemporaryFile:
temp: <open file '<fdopen>', mode 'w+b' at 0x1004486f0>
temp.name: <fdopen>
默認(rèn)情況下使用w+b權(quán)限創(chuàng)建文件,在任何平臺中都是如此,并且程序可以對它進(jìn)行讀寫。這個(gè)例子說明了普通創(chuàng)建文件的方法與TemporaryFile()的不同之處,注意:用TemporaryFile()創(chuàng)建的文件沒有文件名
$ python tempfile_TemporaryFile.py
Building a file name yourself:
temp: <open file '/tmp/guess_my_name.14932.txt', mode 'w+b' at 0x1004481e0>
temp.name: /tmp/guess_my_name.14932.txt
TemporaryFile:
temp: <open file '<fdopen>', mode 'w+b' at 0x1004486f0>
temp.name: <fdopen>
默認(rèn)情況下使用w+b權(quán)限創(chuàng)建文件,在任何平臺中都是如此,并且程序可以對它進(jìn)行讀寫。
實(shí)例二:
import os
import tempfile
temp = tempfile.TemporaryFile()
try:
temp.write('Some data')
temp.seek(0)
print temp.read()
finally:
temp.close()
寫入侯,需要使用seek(),為了以后讀取數(shù)據(jù)。
輸出:
$ python tempfile_TemporaryFile_binary.py
Some data
如果你想讓文件以text模式運(yùn)行,那么在創(chuàng)建的時(shí)候要修改mode為'w+t'。
實(shí)例三:
import tempfile
f = tempfile.TemporaryFile(mode='w+t')
try:
f.writelines(['first\n', 'second\n'])
f.seek(0)
for line in f:
print line.rstrip()
finally:
f.close()
輸出:
$ python tempfile_TemporaryFile_text.py
first
second
tempfile.NamedTemporaryFile
如果臨時(shí)文件會(huì)被多個(gè)進(jìn)程或主機(jī)使用,那么建立一個(gè)有名字的文件是最簡單的方法。這就是NamedTemporaryFile要做的,可以使用name屬性訪問它的名字
import os
import tempfile
temp = tempfile.NamedTemporaryFile()
try:
print 'temp:', temp
print 'temp.name:', temp.name
finally:
# Automatically cleans up the file
temp.close()
print 'Exists after close:', os.path.exists(temp.name)
盡管文件帶有名字,但它仍然會(huì)在close后自動(dòng)刪除
輸出:
$ python tempfile_NamedTemporaryFile.py
temp: <open file '<fdopen>', mode 'w+b' at 0x1004481e0>
temp.name: /var/folders/9R/9R1t+tR02Raxzk+F71Q50U+++Uw/-Tmp-/tmp0zHZvX
Exists after close: False
tempfile.mkdtemp
創(chuàng)建臨時(shí)目錄,這個(gè)不多說,直接看例子:
import os
import tempfile
directory_name = tempfile.mkdtemp()
print directory_name
# Clean up the directory yourself
os.removedirs(directory_name)
輸出
$ python tempfile_mkdtemp.py
/var/folders/9R/9R1t+tR02Raxzk+F71Q50U+++Uw/-Tmp-/tmpB1CR8M
注意:目錄需要手動(dòng)刪除。
Predicting Names
用3個(gè)參數(shù)來控制文件名,名字產(chǎn)生公式:dir + prefix + random + suffix
實(shí)例:
import tempfile
temp = tempfile.NamedTemporaryFile(suffix='_suffix',
prefix='prefix_',
dir='/tmp',
)
try:
print 'temp:', temp
print 'temp.name:', temp.name
finally:
temp.close()
輸出:
$ python tempfile_NamedTemporaryFile_args.py
temp: <open file '<fdopen>', mode 'w+b' at 0x1004481e0>
temp.name: /tmp/prefix_UyCzjc_suffix
tempfile.mkstemp([suffix=''[, prefix='tmp'[, dir=None[, text=False]]]])
mkstemp方法用于創(chuàng)建一個(gè)臨時(shí)文件。該方法僅僅用于創(chuàng)建臨時(shí)文件,調(diào)用tempfile.mkstemp函數(shù)后,返回包含兩個(gè)元素的元組,第一個(gè)元素指示操作該臨時(shí)文件的安全級別,第二個(gè)元素指示該臨時(shí)文件的路徑。參數(shù)suffix和prefix分別表示臨時(shí)文件名稱的后綴和前綴;dir指定了臨時(shí)文件所在的目錄,如果沒有指定目錄,將根據(jù)系統(tǒng)環(huán)境變量TMPDIR, TEMP或者TMP的設(shè)置來保存臨時(shí)文件;參數(shù)text指定了是否以文本的形式來操作文件,默認(rèn)為False,表示以二進(jìn)制的形式來操作文件。
tempfile.mktemp([suffix=''[, prefix='tmp'[, dir=None]]])
mktemp用于返回一個(gè)臨時(shí)文件的路徑,但并不創(chuàng)建該臨時(shí)文件。
tempfile.tempdir
該屬性用于指定創(chuàng)建的臨時(shí)文件(夾)所在的默認(rèn)文件夾。如果沒有設(shè)置該屬性或者將其設(shè)為None,Python將返回以下環(huán)境變量TMPDIR, TEMP, TEMP指定的目錄,如果沒有定義這些環(huán)境變量,臨時(shí)文件將被創(chuàng)建在當(dāng)前工作目錄。
tempfile.gettempdir()
gettempdir()則用于返回保存臨時(shí)文件的文件夾路徑。
相關(guān)文章
Python實(shí)現(xiàn)全角半角字符互轉(zhuǎn)的方法
大家都知道在自然語言處理過程中,全角、半角的的不一致會(huì)導(dǎo)致信息抽取不一致,因此需要統(tǒng)一。這篇文章通過示例代碼給大家詳細(xì)的介紹了Python實(shí)現(xiàn)全角半角字符互轉(zhuǎn)的方法,有需要的朋友們可以參考借鑒,下面跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11pandas讀取HTML和JSON數(shù)據(jù)的實(shí)現(xiàn)示例
Pandas可以直接讀取html和JSON數(shù)據(jù),本文就來介紹一下pandas讀取HTML和JSON數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),感興趣的可以了解一下2024-01-01python基于itchat實(shí)現(xiàn)微信群消息同步機(jī)器人
本篇文章主要介紹了python基于itchat實(shí)現(xiàn)微信群消息同步機(jī)器人,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02python 求一個(gè)列表中所有元素的乘積實(shí)例
今天小編就為大家分享一篇python 求一個(gè)列表中所有元素的乘積實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python實(shí)現(xiàn)簡單ftp客戶端的方法
這篇文章主要介紹了python實(shí)現(xiàn)簡單ftp客戶端的方法,涉及ftplib模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06