<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Ops运维 | Mr`kang Blog｜小豆子]]></title> 
<link>https://www.cnmysql.com/index.php</link> 
<description><![CDATA[关注服务器运维技术、DB优化、Docker虚拟化、自动化运维、工作积累]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Ops运维 | Mr`kang Blog｜小豆子]]></copyright>
<item>
<link>https://www.cnmysql.com/post//</link>
<title><![CDATA[ Python中的fileinput模块和tempfile模块]]></title> 
<author>kangyang &lt;kangyang315@qq.com&gt;</author>
<category><![CDATA[Python]]></category>
<pubDate>Mon, 04 Aug 2014 06:47:17 +0000</pubDate> 
<guid>https://www.cnmysql.com/post//</guid> 
<description>
<![CDATA[ 
	<span style="font-size: 12px;"><span style="font-family: 宋体;">Python中的fileinput模块和tempfile模块<br/>fileinput模块提供处理一个或多个文本文件的功能, 可以通过使用for..in来循环读取一个或多个文本文件内容.<br/>import fileinput<br/>import sys<br/>import glob<br/>import string<br/><br/>for line in fileinput.input("test.txt"):&nbsp;&nbsp;&nbsp;&nbsp; #处理一个文本文件<br/>&nbsp;&nbsp;&nbsp;&nbsp;print line<br/><br/>for line in fileinput.input(glob.glob("samples/*.txt")):&nbsp;&nbsp;#处理多个文本文件<br/>&nbsp;&nbsp;&nbsp;&nbsp;if fileinput.isfirstline():<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "------ reading %s ------&#92;n" % fileinput.filename()&nbsp;&nbsp;&nbsp;&nbsp;#获取文件名称<br/>&nbsp;&nbsp;&nbsp;&nbsp;print str(fileinput.lineno()) + " " + string.upper(line)&nbsp;&nbsp;#获取行号和内容, 注意这里的行号是继前面文件行号来处理的,而不是重新开始计算<br/><br/><br/>tempfile模块提供给你一个快速创建一个命名唯一的临时文件<br/> <br/>import tempfile<br/>import os<br/>tempf = tempfile.mktemp(dir="D:/")<br/>print "tempfile", "=>", tempf<br/>file = open(tempf, "w+b")<br/>file.write("*" * 1000)<br/>file.seek(0)<br/>print len(file.read()), "bytes"<br/>file.close()<br/>try:<br/>&nbsp;&nbsp;&nbsp;&nbsp;os.remove(tempf)&nbsp;&nbsp;&nbsp;&nbsp;#用完以后要注意删除临时文件<br/>except OSError:<br/>&nbsp;&nbsp;&nbsp;&nbsp;pass<br/><br/>当然你还可以通过file = tempfile.TemporaryFile()直接为你创建一个临时文件,并且打开.</span></span>
]]>
</description>
</item><item>
<link>https://www.cnmysql.com/post//#blogcomment</link>
<title><![CDATA[[评论]  Python中的fileinput模块和tempfile模块]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.cnmysql.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>