python的文件处理学习笔记

 python的文件处理函数是open()

以下主要是关于这个函数的一些学习笔记

1、文件处理离不开编码

要注意的是文件打开时的编码和文件保存时的编码的统一,这样才能保证你打开的文件不会存在乱码

总结:创建文件、打开读取文件、写文件保存文件时的编码必须统一,否则可能存在乱码问题

 

2、操作文件时要有始有终

意思就是当你打开文件的时候,要记得关闭文件操作。否则会浪费系统的资源。

 

3、open函数(重点)

(1)、open的语法

open('文件路径','操作模式','编码')

例子:

new_file = open('newfile.txt','w',encoding='utf-8')  
#这里是以utf8编码,以写的模式打开文件newfile.txt,new_file为在系统中获得的文件句柄,只有获得了文件句柄才能操作文件 data = 'my test data' new_file.write(data) new_file.close()

(2)、open函数的文件操作模式

r模式:只读模式,是open函数的默认模式,如果不是特殊指定,默认以r模式打开。

w模式:只写模式,指定了此种模式的话文件只能写,不能读。

注意:如果只有w模式的话,打开文件的时候会清空文件内容重新开始写操作。如果文件不存在就创建

x模式:只写模式,只能写不能读,和w模式的区别就在,如果文件存在就报错,不存在会创建新文件

a模式:追加模式,指定此模式的话文件是以最佳的模式在文件最末尾开始写操作。

 

r+模式:读写模式,此模式下文件既可以读又可以写。

w+模式:写读模式,此模式下可以写也可以读,

注意的是,指定这种模式的话也会清空文件从新开始写。如果文件不存在就创建。

x+模式:写读模式

注意:和w+的区别就是,如果文件存在就报错,如果不存在就重新创建

a+模式:写读追加模式:

 

rb模式:以二进制的模式进行读文件操作

wb模式:以二进制的模式进行写操作

xb模式:以二进制的模式进行写操作

ab模式:以二进制的模式进行追加写操作

注意1:如果你写的代码存在跨平台的话,就要以二进制的模式进行读写操作。

注意2:如果要以二进制的模式进行读写操作,内容需要进行编码转换才便于直观的读写。

例子:

new_file = open('newfile1.txt','wb')
data = '这里我要写内容了,test data\n'
new_file.write(data.encode('utf-8'))
new_file.close()

(3)、文件内置方法flush

这里要清楚的一个概念是,文件的数据操作,都是在内存进行的,如果没有flush或者close的话,是不会落地到文件的。

例子:

import time
with open('newfile.txt','r+') as f:
    list = ['aaaaa\n','bbbbbb\n','ccccc\n','ddddd\n']
    for i in list:
        f.write(i)
        f.flush()
        time.sleep(2)

以上例子就可以看出效果每写一行,你在打开的文件刷新一下就能看到数据落地到文件。  

 

(4)、TextIOWrapper的相关方法

def close(self):  #保存和关闭文件
        """Flush and close this stream.

        :rtype: None
        """
        pass

    def fileno(self): # 返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。
        """Return the underlying file descriptor (an integer) of the stream if
        it exists.

        :rtype: int
        """
        return 0

    def flush(self): # 将内存的数据刷新到文件里
        """Flush the write buffers of the stream if applicable.

        :rtype: None
        """
        pass

    def isatty(self): # 如果文件连接到一个终端设备返回 True,否则返回 False。
        """Return True if the stream is interactive (i.e., connected to a
        terminal/tty device).

        :rtype: bool
        """
        return False

    def readable(self): # 判断文件当前状态是否刻度
        """Return True if the stream can be read from.

        :rtype: bool
        """
        return False

    def readline(self, limit=-1): # 读取整行,包括‘\n’字符
        """Read and return one line from the stream.

        :type limit: numbers.Integral
        :rtype: unicode
        """
        pass

    def readlines(self, hint=-1): # 读取所有行,并以列表的形式返回
        """Read and return a list of lines from the stream.

        :type hint: numbers.Integral
        :rtype: list[unicode]
        """
        return []

    def seek(self, offset, whence=io.SEEK_SET): #获取当前光标所在的位置
        """Change the stream position to the given byte offset.

        :type offset: numbers.Integral
        :type whence: numbers.Integral
        :rtype: None
        """
        pass

    def seekable(self): # 是否可获取文件当前的位置
        """Return True if the stream supports random access.

        :rtype: bool
        """
        return False

    def tell(self): # 返回文件当前位置
        """Return the current stream position.

        :rtype: int
        """
        return 0

    def truncate(self, size=None): #截取文件,截取的字节通过死泽指定。默认为当前的文件位置
        """Resize the stream to the given size in bytes (or the current
        position if size is not specified).

        :type size: numbers.Integral | None
        :rtype: None
        """
        pass

    def writable(self): #文件当前状态是否可写
        """Return True if the stream supports writing.

        :rtype: bool
        """
        return False

    def writelines(self, lines): # 向文件写入一个序列字符串列表,如果需要换行则要加入每一行的换行符。
        """Write a list of lines to the stream.

        :type lines: collections.Iterable[unicode]
        :rtype: None
        """
        pass

 下面有关一个seek获取文件最后一行的操作例子:

with open('newfile.txt','rb+') as f:
    #for i in f:      #这里的for循环是可以拿掉的,如果文件很大最好加上这个for循环
        offs = -10    #设置一个偏移量
        while True:
            f.seek(offs,2)  # 通过偏移量从文件的末尾往前读文件,
            data = f.readlines() #将读到的文件以列表的形式返回
            if len(data) > 1: #大于1代表已经读取到了最后一行和倒数第二行的某几个字节,这样取列表最后一个元素就是文件的最后一行
                print("文件的最后一行:%s" %(data[-1].decode('utf-8')))
                break
            offs *= 2 #如果等于1 那就有可能偏移量太小而导致最后一行读不全。

  

4、让系统帮我们自动关闭文件句柄

这里就要用到with open('文件路径','操作模式') as f:

例子:

with open('newfile1.txt','rb+') as f:
data = f.read()
print(data.decode('utf-8'))

  

 

posted on 2017-04-10 16:26  小强灰灰  阅读(330)  评论(0编辑  收藏  举报

导航