⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 35.html

📁 Python Ebook Python&XML
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; File Handling</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="22.html" class="navtitle">2. Language Review</a> &gt; <span class="nonavtitle">File Handling</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="34.html" title="Input and Output"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=35" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="35.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="36.html" title="Summary"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A20%3A28+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162146122163093009182053228</font><a href="read0.asp?bookname=0672319942&snode=35&now=5%2F31%2F2002+4%3A20%3A28+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>
			
			File Handling</h3>
				<p>Python's core language supports all the basic functions that are necessary to manipulate files. It isn't necessary to import any modules to use them. Whenever you use the open function to get access to a file, Python creates a file object that supports all the built-in methods that apply to this new object.</p>

				
					<H4>
				
				Opening a File</H4>
					<P>
						<A name="idx1073742614"></A>basic syntax: <TT Class="monofont">file = open (</TT>
						<I>
							<Tt class="monofont">filename[, mode[, buffersize]]</tt>
						</i>
						<tt class="monofont">)</tt>
					</p>

					<p>The mode can be <Tt cLass="monofont">r,</Tt>
						<a naMe="idx1073742615"></a>
						<a NAME="idx1073742616"></a>
						<a naME="idx1073742617"></A>
						<Tt claSS="monofont">w,</TT> or <a namE="idx1073742618"></A>
						<A Name="idx1073742619"></a>
						<a name="idx1073742620"></a>
						<tt class="monofont">a</tt> (<tT clAss="monofont">read, write,</tT> and <tt clAss="monofont">append,</tT> respectively). If none of them are mentioned, <A NAme="idx1073742621"></a>
						<a NAME="idx1073742622"></a>
						<a naME="idx1073742623"></A>
						<Tt claSS="monofont">read</TT> mode is assumed.</p>

					<p>If you are working with a binary file, add the letter <tt class="monofont">b</tt> to the mode indicator (for example, <tt class="monofont">rb</tt> or <Tt cLass="monofont">wb</Tt>). The <a naMe="idx1073742624"></a>
						<a NAME="idx1073742625"></a>
						<a naME="idx1073742626"></A>
						<Tt claSS="monofont">b</TT> stands for binary mode (<a namE="idx1073742627"></A>
						<A Name="idx1073742628"></a>text translation mode).</p>

					<p>You can also place a <a name="idx1073742629"></a>
						<a name="idx1073742630"></a>
						<a nAme="idx1073742631"></A>
						<tt cLass="monofont">+</tT> sign to the mode letter to indicate a <tt cLASS="monofont">read/write open</tt> (for example, <tt CLASs="monofont">r+</tt> or <tT CLAss="monofont">w+</tt>)梚t is useful when you need to perform both operations (read and write) in the file. Remember that if you use <TT CLass="monofont">w+,</tt> it will first truncate the file length to zero.</p>

					<p>The last argument in the <tt class="monofont">open</tt> syntax is the <a namE="idx1073742632"></a>
						<a Name="idx1073742633"></A>
						<a namE="idx1073742634"></a>
						<tt CLASs="monofont">buffersize</tt> clause, which means</p>

					<UL>
<LI>
							<p>
								<tt cLASS="monofont">0 = unbuffered</tt>
							</p>

						</lI>
<LI>
							<P>
								<tt class="monofont">1 = line buffered</tt>
							</p>

						</li>
<li>
							<p>If buffersize is greater than 1, its value is equal to the buffer size, in bytes.</p>

						</li>
<li>
							<P>If negative, the buffer size is the system default(default behavior).</p>

						</lI>
</ul>
					<p>Here's an example:</P>

					<pre>
						
file = open("foo.txt", "r")
line = file.readline()
line = line[:-1]        #chop off the newline character
while line:
    print line
    line = file.readline()
    line = line[:-1]
file.close()
				
					</pRe>

				
				
					<h4>
				Supported Methods</h4>
					<P>The following <A NAme="idx1073742635"></a>methods are supported by all file objects.</p>

					
						<H5>
							<TT Class="monofont">read()</TT>
						</H5>
						<P>It reads up to <tt clASS="monofont">n bytes.</Tt> But, if you don't provide any argument, <tt class="monofont">read()</tt> reads all available data from the file.<a name="idx1073742636"></a>
							<a namE="idx1073742637"></a>
						</p>

						<P>
							<a naMe="idx1073742638"></a>basic syntax: <tt ClasS="monofont">file.read(</TT>
							<I>
								<tt clASS="monofont">[nbytes]</Tt>
							</i>
							<tt CLASs="monofont">)</tt>
						</p>

						<PRE>
							
&gt;&gt;&gt; file = open("foo.txt").read()

						</Pre>

						<p>If you say <tt class="monofont">file = open("foo.txt").read(100),</tt> Python will read the file up to its first 100 bytes.</p>

					
					
						<h5>
							<tt clasS="monofont">readline()</tt>
						</H5>
						<p>It reads only one line at a time (until, and including, the <tt Class="monofont">newline</Tt> character).</p>

						<p>
							<A NAMe="idx1073742639"></a>basic syntax: <tt CLASs="monofont">file.readline()</tt>
						</p>

						<PRE>
							
&gt;&gt;&gt; file=open("test.txt","r")
&gt;&gt;&gt; while 1:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -