pyhon中File对象的属性
一个文件被打开后,你有一个file对象,你可以得到有关该文件的各种信息。
以下是和file对象相关的所有属性的列表:
如下实例:
<pre class="brush:html;toolbar:false">#!/usr/bin/python # 打开一个文件 fo = open("foo.txt", "wb") print "Name of the file: ", fo.name print "Closed or not : ", fo.closed print "Opening mode : ", fo.mode print "Softspace flag : ", fo.softspace</pre><span style="color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; white-space: normal; background-color: rgb(255, 255, 255);">以上实例输出结果:</span>
<pre class="brush:html;toolbar:false">Name of the file: foo.txt Closed or not : False Opening mode : wb Softspace flag : 0</pre><span style="color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; white-space: normal; background-color: rgb(255, 255, 255);"></span>
Close()方法
File对象的close()方法刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入。
当一个文件对象的引用被重新指定给另一个文件时,Python会关闭之前的文件。用close()方法关闭文件是一个很好的习惯。
语法:
<pre class="brush:html;toolbar:false">fileObject.close();</pre>例子:
<pre class="brush:html;toolbar:false">#!/usr/bin/python # 打开一个文件 fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # 关闭打开的文件 fo.close()</pre><span style="color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; white-space: normal; background-color: rgb(255, 255, 255);">以上实例输出结果:</span>
<pre class="brush:html;toolbar:false">Name of the file: foo.txt</pre>您可能感兴趣的文章:
pyhon中File对象的属性
php 面向对象详解_对象的串行化和反串行化
如何使用HTML5 File接口在web页面上使用文件下载
CSS滤镜属性详解
访问属性和方法
vbs Size 属性简介
什么是FSO?
CSS手册简编:文本属性
Python __dict__属性:查看对象内部所有属性名和属性值组成的字典
CSS手册简编:表格属性