tempfile

本模块主要提供了产生临时文件或临时目录,支持所有操作系统平台。创建临时文件时,不再使用进程ID来命名,而使用6位随机字符串进行命名。

tempfile.TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None)

返回一个文件类似的临时数据处理对象。它是调用函数mkstemp()来创建文件。当这个临时文件关闭时,就会自动地删除这个文件。参数mode默认是w+b,表示这个文件未关闭之前可以进行读写,同时使用了二进制的方式,以便所有数据都可以被保存进去。参数dir、prefix和suffix都是传送给函数mkstemp()使用。

例子:

#python 3.4

import tempfile

 

fp = tempfile.TemporaryFile()

fp.write(b'blog.csdn.net/caimouse')

fp.seek(0)

print(fp.read())

fp.close()

结果输出如下:

b'blog.csdn.net/caimouse'

 

tempfile.NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True)

本函数也是创建一个临时文件,与函数TemporaryFile()区别是提供了一个可视化的名称,可以通过属性name来获取文件名称。如果这个文件没有删除之前,使用这个名称是可以重新打开这个文件。参数delete是表示文件是否在关闭之后自动删除,如果设置为True则在关闭之后自动删除,否则不会删除。

例子:

#python 3.4

import tempfile

 

with  tempfile.NamedTemporaryFile() as fp:

    fp.write(b'blog.csdn.net/caimouse')

    fp.seek(0)

    print(fp.read())

    print(fp.name)

结果输出如下:

b'blog.csdn.net/caimouse'

C:\Users\tony\AppData\Local\Temp\tmpns5busi8

 

tempfile.SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None)

本函数的功能与TemporaryFile()是一样的,只不过它提供了在内存里缓存数据的功能,只有写入的数据长度大于max_size时,才会写入到文件,或者调用fileno()函数时写入到文件。

例子:

#python 3.4

import tempfile

 

with  tempfile.SpooledTemporaryFile(max_size = 1024) as fp:

    fp.write(b'blog.csdn.net/caimouse')

    fp.seek(0)

    print(fp.read())

    print(fp.name)

结果输出如下:

b'blog.csdn.net/caimouse'

None

 

tempfile.TemporaryDirectory(suffix='', prefix='tmp', dir=None)

本函数使用函数mkdtemp()来创建临时目录。目录的名称可以从name属性取回来。如果想明确地删除,可以调用函数cleanup()来删除。

例子:

#python 3.4

import tempfile

 

with  tempfile.TemporaryDirectory() as tmpDir:   

    print('created temporary directory', tmpDir)

结果输出如下:

created temporary directory C:\Users\tony\AppData\Local\Temp\tmpn1dspyv6

 

tempfile.mkstemp(suffix='', prefix='tmp', dir=None, text=False)

使用最安全的办法创建一个临时文件。参数suffix是用来指明文件名称的后缀;参数prefix是用来指明文件名称的前缀;参数dir是用来指定创建在临时目录下创建的子目录;参数text是用来指明使用文本还是二进制的方式打开。

例子:

#python 3.4

import tempfile

 

f = tempfile.mkstemp()  

print('created temporary directory', f)

结果输出如下:

created temporary directory (3, 'C:\\Users\\tony\\AppData\\Local\\Temp\\tmpzawnzdyj')

 

tempfile.mkdtemp(suffix='', prefix='tmp', dir=None)

使用最安全的方法创建一个临时目录。参数suffix、prefix和dir与上面函数mkstemp()里的参数一样。

例子:

#python 3.4

import tempfile

 

f = tempfile.mkdtemp()  

print('created temporary directory', f)

结果输出如下:

created temporary directory C:\Users\tony\AppData\Local\Temp\tmp85x3176v

 

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

本函数是创建一个临时文件名称,将来不再使用。

 

tempfile.tempdir

这个成员变量保存了临时目录里的路径。如果是空值,那么它会查找系统临时目录来替换它。

例子:

#python 3.4

import tempfile

 

f = tempfile.mkdtemp()  

print('created temporary directory', f)

print(tempfile.tempdir)

结果输出如下:

created temporary directory C:\Users\tony\AppData\Local\Temp\tmp0j9kg77k

C:\Users\tony\AppData\Local\Temp

 

tempfile.gettempdir()

返回当前选择创建临时文件的目录。

例子:

#python 3.4

import tempfile

 

f = tempfile.mkdtemp()  

print('created temporary directory', f)

print(tempfile.tempdir)

print(tempfile.gettempdir())

结果输出如下:

created temporary directory C:\Users\tony\AppData\Local\Temp\tmpzws_qvr4

C:\Users\tony\AppData\Local\Temp

C:\Users\tony\AppData\Local\Temp

 

tempfile.gettempprefix()

返回用来创建临时文件的前缀。

例子:

#python 3.4

import tempfile

 

f = tempfile.mkdtemp()  

print('created temporary directory', f)

print(tempfile.tempdir)

print(tempfile.gettempdir())

print(tempfile.gettempprefix())

结果输出如下:

created temporary directory C:\Users\tony\AppData\Local\Temp\tmpmcpgwryt

C:\Users\tony\AppData\Local\Temp

C:\Users\tony\AppData\Local\Temp

posted @ 2019-09-26 16:53  排骨南  阅读(427)  评论(0编辑  收藏  举报