博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python模块学习——tempfile
阅读量:7200 次
发布时间:2019-06-29

本文共 734 字,大约阅读时间需要 2 分钟。

主要有以下几个函数:

tempfile.TemporaryFile

如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择。其他的应用程序是无法找到或打开这个文件的,因为它并没有引用文件系统表。用这个函数创建的临时文件,关闭后会自动删除。

 1 
import os
 2 
import tempfile
 3  
 4 
print 
'
Building a file name yourself:
'
 5 filename = 
'
/tmp/guess_my_name.%s.txt
' % os.getpid()
 6 temp = open(filename, 
'
w+b
')
 7 
try:
 8     
print 
'
temp:
', temp
 9     
print 
'
temp.name:
', temp.name
10 
finally:
11     temp.close()
12     os.remove(filename)     
#
 Clean up the temporary file yourself
13 
 
14 
print
15 
print 
'
TemporaryFile:
'
16 temp = tempfile.TemporaryFile()
17 
try:
18     
print 
'
temp:
', temp
19     
print 
'
temp.name:
', temp.name
20 
finally:
21     temp.close()  
#
 Automatically cleans up the file

转载于:https://www.cnblogs.com/juxuan/p/3531577.html

你可能感兴趣的文章
MySQL5.7杀手级新特性:GTID原理与实战
查看>>
波浪刻度电池View
查看>>
爬取阳光问政平台
查看>>
Maven: Usage
查看>>
Kotlin入门(22)适配器的简单优化
查看>>
代码外的任务很精彩 ---Software project survival guide 读书报告
查看>>
Java io流学习总结(二)
查看>>
PHP中的PDO函数库详解
查看>>
C++编程常见错误
查看>>
Converter of C#&VB.NET
查看>>
修改ASPCMS升级扩展功能
查看>>
模拟HTTP请求的返回&shell写cgi
查看>>
问题tips
查看>>
如何给NSMutableArray排序
查看>>
CF991C Candies 二分 第十五
查看>>
查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)
查看>>
sqlmap注入之tamper绕过WAF防火墙过滤
查看>>
如何用Tacker将NFV带入OpenStack?
查看>>
快速排序简明教程
查看>>
【转】Unity3D学习日记(二)使用UGUI制作虚拟摇杆控制摄像机
查看>>