码迷,mamicode.com
首页 > 其他好文 > 详细

文件操作

时间:2016-08-20 11:37:00      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

此文转至: http://www.cnblogs.com/resn/articles/5789720.html

文件操作
操作文件的步骤: 打开文件--》操作文件--》关闭文件

 

f= open("b.txt","r")  # 打开文件
f.close()  # 关闭文件

f:文件描述符其实是一个索引值,系统核心为每一个进程维护该进程打开文件的记录表。每打开一个文件,就返回一个文件描述符。

 

 

 

 

平时写word文档的时候,对文件内容的操作有 增删改查,。从本质上来说,只有两种操作 一个是读(查),一个写(增删改)

读、写,

Read()
Readline()
Readlines ()
write()  将文本写入文件,并返回文本长度
writelines()  向文件中写入一序列的字符串。(下次写换行?)

 

 

‘r’ 只读模式,默认
‘w’ 只写模式,不可读,不存在则创建,存在则删除内容
‘x’ 创建一个文件并打开,进入只写模式
‘a‘ 追加模式。不存在则创建;存在则只追加内容
‘b’ 二进制模式 
‘t’ 文本模式,默认
‘+’ 升级只读和只写模式,都变得可读写
‘U’ 在读写时,可以将 \r\n 自动转换成\n

r+ w+ a+ rb wb ab rU r+U

 

操作文件的方法

close(self, /) 关闭文件
Flush and close the IO object. 
This method has no effect if the file is already closed.

detach(self, /) 增加或改变已打开文件的编码
在不关闭一个已打开的文件前提下增加或改变它的Unicode编码
编解码Unicode
import io
fo = open("c.txt", "r+")
print(fo)
fo = io.TextIOWrapper(fo.detach(), encoding=‘latin-1‘)
print(fo)
fo.close()
Separate the underlying buffer from the TextIOBase and return it. 
After the underlying buffer has been detached, the TextIO is in an
unusable state.

fileno(self, /) 返回文件描述符的名字
Returns underlying file descriptor if one exists. 
OSError is raised if the IO object does not use a file descriptor.

flush(self, /)
刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 
而不是被动的等待输出缓冲区写入
Flush write buffers, if applicable. 
This is not implemented for read-only and non-blocking streams.

isatty(self, /) isatty() 方法检测文件是否连接到一个终端设备,
如果是返回 True,否则返回 False。 网络编程的时候讲
Return whether this is an ‘interactive‘ stream. 
Return False if it can‘t be determined.

read(self, size=-1, /) 读取n个字符,不指定时,读取真个文件
Read at most n characters from stream. 
Read from underlying buffer until we have n characters or we hit EOF.
If n is negative or omitted, read until EOF.

 

 

readable(self, /) 判断文件是否可读
Return whether object was opened for reading. 
If False, read() will raise OSError.

readline(self, size=-1, /) 读取一行
Read until newline or EOF. 
Returns an empty string if EOF is hit immediately.

seek(self, cookie, whence=0, /) 移动指针到指定位置
Change stream position. cookie表示偏移的位置, whence表示从哪里开始偏移
Change the stream position to the given byte offset. The offset is
interpreted relative to the position indicated by whence. Values
for whence are: 
whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;
0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
* 0 -- start of stream (the default); offset should be zero or positive
* 1 -- current stream position; offset may be negative
* 2 -- end of stream; offset is usually negative 
Return the new absolute position.

seekable(self, /) 判断文件是否支持随机访问
Return whether object supports random access. 
If False, seek(), tell() and truncate() will raise OSError.
This method may need to do a test seek().

tell(self, /) 返回文件指针的当前位置
Return current stream position.

truncate(self, pos=None, /) 截断数据,仅保留指定之前数据
从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;
截断之后n后面的所有字符被删除。其中win下的换行代表2个字符大小。
Truncate file to size bytes. 
File pointer is left unchanged. Size defaults to the current IO
position as reported by tell(). Returns the new size.

writable(self, /) 判断文件是否可写
Return whether object was opened for writing. 
If False, write() will raise OSError.

write(self, text, /) 将文本写入文件,并返回文本长度
Write string to stream.
Returns the number of characters written (which is always equal to
the length of the string).

readlines(self, hint=-1, /)
读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 
实际读取值可能比sizhint较大, 因为需要填充缓冲区。
Return a list of lines from the stream. 
hint can be specified to control the number of lines read: no more
lines will be read if the total size (in bytes/characters) of all
lines so far exceeds hint.

writelines(self, lines, /)
向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。

 

 

with语句

With open() as f:
……

with open(‘file1‘) as f1:
with open(‘file2‘) as f2:
with open(‘file3‘) as f3:
for i in f1:
j = f2.readline()
k = f3.readline()
print(i,j,k)

With open() as f1, open() as f2:
print(f1.realine())
print(f2.realine())

 

文件描述符 for 的使用
for i in f
…..内存中只保存一行

 

 

去除文件内的空行
两个空行合并成一行
文件内每行的首字母变为大写
将一个文件的偶数行写入另一个文件
文本文件score.dic 中存储了n名学生的信息(班级编号,姓名,成绩),每个学生信息占一行,每行的数据之间使用制表符分割,如下所示:
145811  fuxin 100
145811  chengxian 90
145812  zhangxue 92
145812  lijun 88
文件中存储的学生信息按照座位编号升序排列,每一排的人数可以不同,要求读取文件中所有学生的成绩,计算每一排的平均成绩,将座位排号和平均成绩输出。

Haproxy文件的查询删除
Haproxy文件操作 增删改查询

相对路径 和绝对路径

文件操作

标签:

原文地址:http://www.cnblogs.com/tiger666/p/5789744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!