Python ——tempfile
主要有以下几个函数:
tempfile.TemporaryFile
如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择。其他的应用程序是无法找到或打开这个文件的,因为它并没有引用文件系统表。用这个函数创建的临时文件,关闭后会自动删除。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import osimport tempfileprint '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.namefinally: temp.close() os.remove(filename) # Clean up the temporary file yourselfprintprint 'TemporaryFile:'temp = tempfile.TemporaryFile()try: print 'temp:', temp print 'temp.name:', temp.namefinally: temp.close() # Automatically cleans up the file |
这个例子说明了普通创建文件的方法与TemporaryFile()的不同之处,注意:用TemporaryFile()创建的文件没有文件名
$ 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>
默认情况下使用w+b权限创建文件,在任何平台中都是如此,并且程序可以对它进行读写。
|
1
2
3
4
5
6
7
8
9
10
11
|
import osimport tempfiletemp = 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 tempfilef = 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 osimport tempfiletemp = tempfile.NamedTemporaryFile()try: print 'temp:', temp print 'temp.name:', temp.namefinally: # 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 osimport tempfiledirectory_name = tempfile.mkdtemp()print directory_name# Clean up the directory yourselfos.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 tempfiletemp = tempfile.NamedTemporaryFile(suffix='_suffix', prefix='prefix_', dir='/tmp', )try: print 'temp:', temp print 'temp.name:', temp.namefinally: 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指定了临时文件所在的目录,如果没有指定目录,将根据系统环境变量TMPDIR, TEMP或者TMP的设置来保存临时文件;参数text指定了是否以文本的形式来操作文件,默认为False,表示以二进制的形式来操作文件。
tempfile.mktemp([suffix=''[, prefix='tmp'[, dir=None]]])
mktemp用于返回一个临时文件的路径,但并不创建该临时文件。
tempfile.tempdir
该属性用于指定创建的临时文件(夹)所在的默认文件夹。如果没有设置该属性或者将其设为None,Python将返回以下环境变量TMPDIR, TEMP, TEMP指定的目录,如果没有定义这些环境变量,临时文件将被创建在当前工作目录。
tempfile.gettempdir()
gettempdir()则用于返回保存临时文件的文件夹路径。
Python ——tempfile的更多相关文章
- Python tempfile (临时文件)
Python tempfile 大量临时数据放在内存中会占用大量资源,可以使用临时文件来进行储存 临时文件不用命名,且使用后会被自动删除 TemporaryFile 使用 TemporaryFile ...
- pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)
pig 脚本运行不需要后缀名 pig脚本名为tempfile,无后缀名 用pig -f tempfile 可直接运行 另外,pig tempfile也可以直接运行 这样就可以用python临时文件存储 ...
- python tempfile 创建临时目录
一.tempfile介绍 该模块创建临时文件和目录.它适用于所有支持的平台.TemporaryFile,NamedTemporaryFile,TemporaryDirectory,和SpooledTe ...
- python开发_tempfile
python中的tempfile模块,是为创建临时文件(夹)所提供的 如果你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么tempfile模块来创建临时文件(夹)是个不错的选择 其 ...
- [python] 创建临时文件-tempfile模块
This module generates temporary files and directories. It works on all supported platforms.In versio ...
- 关于python 文件操作os.fdopen(), os.close(), tempfile.mkstemp()
嗯.最近在弄的东西也跟这个有关系,由于c基础渣渣.现在基本上都忘记得差不多的情况下,是需要花点功夫才能弄明白. 每个语言都有相关的文件操作. 今天在flask 的例子里看到这样一句话.拉开了文件操作折 ...
- Python模块学习——tempfile
主要有以下几个函数: tempfile.TemporaryFile 如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择.其 ...
- 善用tempfile库创建python进程中的临时文件
技术背景 临时文件在python项目中时常会被使用到,其作用在于随机化的创建不重名的文件,路径一般都是放在Linux系统下的/tmp目录.如果项目中并不需要持久化的存储一个文件,就可以采用临时文件的形 ...
- Python标准库tempfile的使用总结
Python标准库tempfile的使用总结 临时文件是计算机程序存储临时数据的文件,它的扩展名通常是".temp".本文用于记录使用Python提供的临时文件API解决实际问题的 ...
随机推荐
- 一道Oracle子查询小练习
一道Oracle子查询小练习 昨天晚上躺在床上看Oracle(最近在学习这个),室友说出个题目让我试试.题目如下: 有如下表结构,请选择出成绩为前三名的人的信息(如果成绩相同,则算并列),表名为t ...
- JVM中堆栈
1.JVM是基于堆栈的虚拟机.JVM为每个新创建的线程都分配一个堆栈.也就是说,对于一个Java程序来说,它的运行就是通过对堆栈的操作来完成的.堆栈以帧为单位保存线程的状态.JVM对堆栈只进行两种操作 ...
- thinkphp 规则路由
规则路由是一种比较容易理解的路由定义方式,采用ThinkPHP设计的规则表达式来定义. 规则表达式 规则表达式通常包含静态地址和动态地址,或者两种地址的结合,例如下面都属于有效的规则表达式: 'my' ...
- dp转图论——cf1070A好题
dp的状态转移很像一张有向图:每个状态为一个点,每中转移方案是一条有向边 本题要求是求出最小的数,那我们用状态[i,j]表示模i,数位和为j,那么从每个点出发的十条有向边代表[0,9]十个数 从每个状 ...
- Ubuntu-WPS无法输入中文
WPS无法输入中文 原因:环境变量未正确设置 $ vi /usr/bin/wps,添加以下内容: #!/bin/bash export XMODIFIERS="@im=fcitx" ...
- sql 查询问题
在做数据导出时候,当某个表某字段含有单引号时候老是报错,所以要排除这种情况: sql查询某表某字段值带单引号情况 select 主键码 from 馆藏书目库 where 题名 like '%''%' ...
- 百度跨域搜索demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化
JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化 1使用URLRewrite实现url地址伪静态化1.1URLRewirte的用处 1.满足搜索引擎的要求. 2.隐藏技术实现,提 ...
- 矩阵连乘 /// 区间DP oj1900
题目大意: 输入t :t为测试用例个数 接下来t个测试 每个测试用例 第一行输入n: n为矩阵个数 保证n个矩阵依序是可乘的 接下来n行 每行输入p,q:p为长度q为宽度 对给定的n个矩阵确定一个计算 ...
- Eclipse 连接MySql数据库总结
Eclipse 连接MySql数据库总结 一.在MySql中创建数据库,并创建表,向表中插入数据 1.创建数据库 create database select_test 2.创建表 create ta ...