python 的tempfile学习
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)
print "buildint a file using tempfile"
t = tempfile.TemporaryFile()
try:
print 'temp:',temp
print 'temp.name',temp.name
finally:
temp.close()
building a file name yourself:
temp: <open file '/tmp/guess_my_name.24462.txt', mode 'w+b' at 0x103567db0>
temp.name /tmp/guess_my_name.24462.txt
buildint a file using tempfile
temp: <closed file '/tmp/guess_my_name.24462.txt', mode 'w+b' at 0x103567db0>
temp.name /tmp/guess_my_name.24462.txt
使用tempfile.TemporarFile函数来创建的临时文件,其他的应用程序无法找到或打开这个文件,因为它并没有引用文件系统表。
这个函数创建的临时文件,关闭后自动删除。
=============
tempfile.TemporarFile默认情况下使用w+b权限来创建文件;
使用temp.seek来重定位,方便以后读取数据
import os
import tempfile temp = tempfile.TemporaryFile() try:
temp.write('some data')
temp.seek(0) print temp.read()
finally:
temp.close()
===========
python 的tempfile学习的更多相关文章
- Python 装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- 从Theano到Lasagne:基于Python的深度学习的框架和库
从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...
- Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习
http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...
- (转载)Python装饰器学习
转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...
- python网络爬虫学习笔记
python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...
- Python装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...
- Python的基础学习(第二周)
模块初始 sys模块 import sys sys.path #打印环境变量 sys.argv#打印该文件路径 #注意:该文件名字不能跟导入模块名字相同 os模块 import os cmd_res ...
- Python第九课学习
Python第九课学习 数据结构: 深浅拷贝 集合set 函数: 概念 创建 参数 return 定义域 www.cnblogs.com/yuanchenqi/articles/5782764.htm ...
随机推荐
- Linux开发常见问题:GCC:链接器输入文件未使用,因为链接尚未完成
问:我在Linux中运行一个make文件去编译C语言代码,然后得到了如下的错误信息: gcc -Wall -fPIC -DSOLARIS -DXP_UNIX -DMCC_HTTPD -D_ ...
- Mysql常见的引擎
常用的引擎是:Innodb和Myiasm这两种引擎: innodb: 提供了对事务的ACID操作,还提供了行级锁和外键约束,,他的优势就是处理大量数据,在msql启动的时候,首先会建立一个缓存池,主要 ...
- git中Bash基本操作命令
).cd : 改变目录. ).cd . . 回退到上一个目录,直接cd进入默认目录 ).pwd : 显示当前所在的目录路径. ).ls(ll): 都是列出当前目录中的所有文件,只不过ll(两个ll)列 ...
- C#中类的成员
一.C#中类的成员 1. 类的成员 类中的数据和函数都称为类的成员.类的成员可以分为两类: ?类本身所声明的. ?从基类中继承来的. 如果在类声明中没有指定基类,则该类将继承System.Object ...
- POJ2154 Color(Polya定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11654 Accepted: 3756 Description Bead ...
- 转:比较spring cloud和dubbo,各自的优缺点是什么
原文:https://blog.csdn.net/u010664947/article/details/80007767 dubbo由于是二进制的传输,占用带宽会更少 springCloud是http ...
- 描述linux目录结构以及目录结构命名规定
FHS全称(Filesystem Hierarchy Standard),中文意思是目录层次标准,是linux的目录规范标准. 详情点击查看 FHS定义了两层规范: 第一层:“/”目录下的各个目录应该 ...
- Jongmah CodeForces - 1110D
传送门 题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 题解:三个一模一样的 ...
- ZOJ3640 概率DP
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...
- Codeforces Round 97B 点分治
B. Superset time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...