Python模塊學習——tempfile

主要有如下幾個函數:html

tempfile.TemporaryFilepython

如何你的應用程序須要一個臨時文件來存儲數據,但不須要同其餘程序共享,那麼用TemporaryFile函數建立臨時文件是最好的選擇。其餘的應用程序是沒法找到或打開這個文件的,由於它並無引用文件系統表。用這個函數建立的臨時文件,關閉後會自動刪除。安全

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
print 'TemporaryFile:'
temp = tempfile.TemporaryFile()
try :
     print 'temp:' , temp
     print 'temp.name:' , temp.name
finally :
     temp.close()   # Automatically cleans up the file

這個例子說明了普通建立文件的方法與TemporaryFile()的不一樣之處,注意:用TemporaryFile()建立的文件沒有文件名dom

 

$ python tempfile_TemporaryFile.py函數

 

Building a file name yourself:ui

temp: <open file '/tmp/guess_my_name.14932.txt', mode 'w+b' at 0x1004481e0>spa

temp.name: /tmp/guess_my_name.14932.txtcode


TemporaryFile:htm

temp: <open file '<fdopen>', mode 'w+b' at 0x1004486f0>blog

temp.name: <fdopen>

 

默認狀況下使用w+b權限建立文件,在任何平臺中都是如此,而且程序能夠對它進行讀寫。

 

?
1
2
3
4
5
6
7
8
9
10
11
import os
import tempfile
 
temp = tempfile.TemporaryFile()
try :
     temp.write( 'Some data' )
     temp.seek( 0 )
     
     print temp.read()
finally :
     temp.close()

寫入侯,須要使用seek(),爲了之後讀取數據。

 

 

$ python tempfile_TemporaryFile_binary.py

 

Some data

若是你想讓文件以text模式運行,那麼在建立的時候要修改mode爲'w+t'

 

?
1
2
3
4
5
6
7
8
9
10
11
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

若是臨時文件會被多個進程或主機使用,那麼創建一個有名字的文件是最簡單的方法。這就是NamedTemporaryFile要作的,可使用name屬性訪問它的名字

 

?
1
2
3
4
5
6
7
8
9
10
11
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)

儘管文件帶有名字,但它仍然會在close後自動刪除

 

$ 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

建立臨時目錄,這個很少說,直接看例子

 

?
1
2
3
4
5
6
7
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

目錄須要手動刪除。

 

Predicting Names

用3個參數來控制文件名,名字產生公式:dir + prefix + random + suffix

 

?
1
2
3
4
5
6
7
8
9
10
11
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方法用於建立一個臨時文件。該方法僅僅用於建立臨時文件,調用tempfile.mkstemp函數後,返回包含兩個元素的元組,第一個元素指示操做該臨時文件的安全級別,第二個元素指示該臨時文件的路徑。參數suffix和prefix分別表示臨時文件名稱的後綴和前綴;dir指定了臨時文件所在的目錄,若是沒有指定目錄,將根據系統環境變量TMPDIRTEMP或者TMP的設置來保存臨時文件;參數text指定了是否以文本的形式來操做文件,默認爲False,表示以二進制的形式來操做文件。

tempfile.mktemp([suffix=''[, prefix='tmp'[, dir=None]]])

    mktemp用於返回一個臨時文件的路徑,但並不建立該臨時文件。

tempfile.tempdir

    該屬性用於指定建立的臨時文件(夾)所在的默認文件夾。若是沒有設置該屬性或者將其設爲None,Python將返回如下環境變量TMPDIR, TEMP, TEMP指定的目錄,若是沒有定義這些環境變量,臨時文件將被建立在當前工做目錄。

tempfile.gettempdir()

    gettempdir()則用於返回保存臨時文件的文件夾路徑。

相關文章
相關標籤/搜索