Python之文件处理

Python之文件处理

不同模式打开文件的完全列表:

模式 描述
r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。一般用于非文本文件如图片等。
r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于非文本文件如图片等。
w 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
w+ 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+ 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+ 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

下图很好的总结了这几种模式:

1.读写文件

读文件:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/13 14:41
# @Author  : Feng Xiaoqing
# @File    : test.py
# @Function: -----------

f = open("test.txt")
text = f.readlines()
print(text)

执行结果:

['aaaaa\n', 'bbbb\n', 'ccc\n', 'ddd']

写文件:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/13 15:41
# @Author  : Feng Xiaoqing
# @File    : test.py
# @Function: -----------

f = open("test.log","w",encoding='utf-8')
f.write("Hello World!")
f.close()

执行结果:

改为把w改为a时的执行结果:

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/13 20:22
# @Author  : Feng Xiaoqing
# @File    : test.py
# @Function: -----------

class File():
    ENCODING = "utf-8"
    def wirteFile(self):
        filename = input("Please input the file name: ")
        f = codecs.open(filename,'a',encoding=File.ENCODING)
        while 1:
            context = input("Please input the file context(quit for exit): ")
            if context == "quit":
                break
            f.write(context)
            f.write("\n")
    def readFile(self):
        print("####################STAT######################")
        readfile = input("Please input your read file name: ")
        fileread = codecs.open(readfile,encoding=File.ENCODING)
        readContext = fileread.read()
        print(readContext)
        print("################### END ######################")
        fileread.close()
if __name__ == '__main__':
    import codecs
    File = File()
    File.wirteFile()
    File.readFile()

执行过程:

Please input the file name: fxq.log
Please input the file context(quit for exit): Hello world!
Please input the file context(quit for exit): My name is Fengxiaoqing
Please input the file context(quit for exit): I'm 30
Please input the file context(quit for exit): I'm a bright boy.l
Please input the file context(quit for exit): Think you very much!
Please input the file context(quit for exit): quit
####################STAT######################
Please input your read file name: fxq.log
Hello world!
My name is Fengxiaoqing
I'm 30
I'm a bright boy.l
Think you very much!
################### END ######################
进程已结束,退出代码0

2. Python中的文件操作方法

# 文件对象f常用的操作方法
# read()       把文件的所有内容都读取出来,返回一个字符串
# write(data)  把字符串data写入到文件中,只接受字符串参数
# fr.readline()   每次读取文件一行数据,返回每行的字符串数据
# fr.readlines()  读取文件内容,返回一个list,每一行是一个元素
# fr.name    文件名字
# fr.fileno()   文件描述符
# fr.close()    关闭文件
# fr.encoding    文件编码
# fr.closed    返回bool值, 判断文件是否已经关闭
# fr.seek(offset, whence)  offset偏移量正数向后偏移,负数向前偏移   whence 0 开头,1 现在位置  2 代表结尾
# fr.tell()       返回文件光标位置
# fr.truncate(size)   只有写文件才可以用,清空文件,size表示清空到什么地方.
# help(fr.seek)  控制文件光标,文件需要使用b方式打开,

with用法:

        with codec.open("1.log",encoding= "utf-8") as f:
        print(f.read())

猜你喜欢

转载自my.oschina.net/u/3804957/blog/1795394