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()则用于返回保存临时文件的文件夹路径。

相关文章
相关标签/搜索