分享

python中的open函数

 怎么了啊早上 2014-04-25

分类: Python/Ruby

打开一个文件并向其写入内容

Python的open方法用来打开一个文件。第一个参数是文件的位置和文件名,第二个参数是读写模式。这里我们采用w模式,也就是写模式。在这种模式下,文件原有的内容将会被删除。

#to write
testFile = open('cainiao.txt','w')
#error testFile.write(u'菜鸟写Python!')
#写入一个字符串
testFile.write('菜鸟写Python!')
#字符串元组
codeStr = ('<div>','<p>','完全没有必要啊!','

','

')
testFile.write('\n\n')
#将字符串元组按行写入文件
testFile.writelines(codeStr)
#关闭文件。
testFile.close()向文件添加内容

在open的时候制定'a'即为(append)模式,在这种模式下,文件的原有内容不会消失,新写入的内容会自动被添加到文件的末尾。

#to append
testFile = open('cainiao.txt','a')
testFile.write('\n\n')
testFile.close()读文件内容

在open的时候制定'r'即为读取模式,使用

#to read
testFile = open('cainiao.txt','r')
testStr = testFile.readline()
print testStr
testStr = testFile.read()
print testStr
testFile.close()在文件中存储和恢复Python对象

使用Python的pickle模块,可以将Python对象直接存储在文件中,并且可以再以后需要的时候重新恢复到内容中。

testFile = open('pickle.txt','w')
#and import pickle
import pickle
testDict = {'name':'Chen Zhe','gender':'male'}
pickle.dump(testDict,testFile)
testFile.close()
testFile = open('pickle.txt','r')
print pickle.load(testFile)
testFile.close()二进制模式

调用open函数的时候,在模式的字符串中使用添加一个b即为二进制模式。

#binary mode
testFile = open('cainiao.txt','wb')
#where wb means write and in binary
import struct
bytes = struct.pack('>i4sh',100,'string',250)
testFile.write(bytes)
testFile.close()

读取二进制文件。

testFile = open('cainiao.txt','rb')
data = testFile.read()
values = struct.unpack('>i4sh',data)
print values1.open使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
[转载]Python <wbr>open读写文件file_object = open('thefile.txt')
[转载]Python <wbr>open读写文件
try:
[转载]Python <wbr>open读写文件 all_the_text
 = file_object.read( )
[转载]Python <wbr>open读写文件
finally:
[转载]Python <wbr>open读写文件 file_object.close( )

注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象file_object无法执行close()方法。
2.读文件读文本文件
[转载]Python <wbr>open读写文件input = open('data', 'r')
[转载]Python <wbr>open读写文件
#第二个参数默认为r
[转载]Python <wbr>open读写文件
input = open('data')

读二进制文件
[转载]Python <wbr>open读写文件input = open('data', 'rb')

读取所有内容
[转载]Python <wbr>open读写文件file_object = open('thefile.txt')
[转载]Python <wbr>open读写文件
try:
[转载]Python <wbr>open读写文件 all_the_text
 = file_object.read( )
[转载]Python <wbr>open读写文件
finally:
[转载]Python <wbr>open读写文件 file_object.close( )

读固定字节
[转载]Python <wbr>open读写文件file_object = open('abinfile', 'rb')
[转载]Python <wbr>open读写文件
try:
[转载]Python <wbr>open读写文件
 while True:
[转载]Python <wbr>open读写文件 chunk
 = file_object.read(100)
[转载]Python <wbr>open读写文件
 if not chunk:
[转载]Python <wbr>open读写文件
 break
[转载]Python <wbr>open读写文件 do_something_with(chunk)
[转载]Python <wbr>open读写文件
finally:
[转载]Python <wbr>open读写文件 file_object.close( )

读每行
[转载]Python <wbr>open读写文件list_of_all_the_lines = file_object.readlines( )

如果文件是文本文件,还可以直接遍历文件对象获取每行:
[转载]Python <wbr>open读写文件for line in file_object:
[转载]Python <wbr>open读写文件 process line

3.写文件写文本文件
[转载]Python <wbr>open读写文件output = open('data', 'w')

写二进制文件
[转载]Python <wbr>open读写文件output = open('data', 'wb')

追加写文件
[转载]Python <wbr>open读写文件output = open('data', 'w ')

写数据
[转载]Python <wbr>open读写文件file_object = open('thefile.txt', 'w')
[转载]Python <wbr>open读写文件file_object.write(all_the_text)
[转载]Python <wbr>open读写文件file_object.close( )

写入多行
[转载]Python <wbr>open读写文件file_object.writelines(list_of_text_strings)

注意,调用writelines写入多行在性能上会比使用write一次性写入要高。



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
w      以写方式打开 (必要时清空)
a      以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r       以读写模式打开
w       以读写模式打开 (参见 w )
a       以读写模式打开 (参见 a )
rb      以二进制读模式打开
wb      以二进制写模式打开 (参见 w )
ab      以二进制追加模式打开 (参见 a )
rb      以二进制读写模式打开 (参见 r )
wb      以二进制读写模式打开 (参见 w )
ab      以二进制读写模式打开 (参见 a )
a.      Python 2.3 中新增

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多