博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python的open函数
阅读量:6943 次
发布时间:2019-06-27

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

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

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语句来确保最后能关闭文件。


file_object = open('thefile.txt')
try:
all_the_text = file_object.read( ) 
finally:
file_object.close( )

注:不能把open语句放在try块里。由于当打开文件出现异常时,文件对象file_object无法运行close()方法。

2.读文件读文本文件
input 
= 
open(
'
data
'
, 
'
r
'
)
#
第二个參数默觉得r
input 
= 
open(
'
data
'
)

读二进制文件
input 
= 
open(
'
data
'
, 
'
rb
'
)

读取全部内容
file_object 
= 
open(
'
thefile.txt
'
)
try
:
 all_the_text
 
= 
file_object.read( )
finally
:
 file_object.close( )

读固定字节
file_object 
= 
open(
'
abinfile
'
, 
'
rb
'
)
try
:
 
while 
True:
 chunk
 
= 
file_object.read(
100
)
 
if 
not 
chunk:
 
break
 do_something_with(chunk)
finally
:
 file_object.close( )

读每行
list_of_all_the_lines 
= 
file_object.readlines( )

假设文件是文本文件,还能够直接遍历文件对象获取每行:

for 
line 
in 
file_object:
 process line

3.写文件写文本文件
output 
= 
open(
'
data
'
, 
'
w
'
)

写二进制文件
output 
= 
open(
'
data
'
, 
'
wb
'
)

追加写文件
output 
= 
open(
'
data
'
, 
'
'
)

写数据
file_object 
= 
open(
'
thefile.txt
'
, 
'
w
'
)
file_object.write(all_the_text)
file_object.close( )

写入多行
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 中新增

http://www.360doc.com/content/14/0425/12/16044571_372066859.shtml

转载地址:http://jjinl.baihongyu.com/

你可能感兴趣的文章
008_线上排障汇总
查看>>
HRMS(人力资源管理系统)-从单机应用到SaaS应用-架构分析(功能性、非功能性、关键约束)-下篇...
查看>>
EF Core 2.1 中的 Eager loading、Explicit loading和LazyLoading (转自MSDN)
查看>>
C# http 性能优化500毫秒到 60 毫秒
查看>>
利用WPF创建含多种交互特性的无边框窗体
查看>>
unitywebrequest-post-url-jsondata
查看>>
【JAVA笔记】JAVA后端实现统一扫码支付:微信篇
查看>>
当你输入一个网址的时候,实际会发生什么?(转载)
查看>>
vc通过webbrowser操作ie元素
查看>>
JavaScript中RegExp.$1是什么意思
查看>>
Python中HTTPS连接
查看>>
基于epoll的简单的httpserver
查看>>
[Java] 简化正则表达式的使用
查看>>
关于echarts的那些事(地图标点,折线图,饼图)
查看>>
ExecutorService 的理解与使用
查看>>
[Javascript Crocks] Flatten Nested Maybes with `chain`
查看>>
【转载并记录】SpringBoot 入门(一)
查看>>
我的第一个python web开发框架(30)——定制ORM(六)
查看>>
performselectoronmainthread
查看>>
产品经理必须要了解的26个文档
查看>>