文件操作(初阶)

操作文件时,一般需要经历如下步骤:

  • 打开文件
  • 操作文件
  • 关闭文件
#open()是python的内置函数,而read()、close()等都是属于TextIOWrapper类的,详情参见源码。

f = open('1.txt', 'r')       #打开文件
data = f.read()           #操作文件
f.close()                #关闭文件
print(data)

一、打开文件

文件在open的时候是不会被加到内存中的,只有read或write的时候才会加到内存中。

打开文件的模式有:

r ,只读模式【默认】
w,只写模式【不可读;不存在则创建;存在则清空内容;】
x, 只写模式【不可读;不存在则创建,存在则报错】
a, 追加模式【可读; 不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件:

r+, 读写【可读,可写】,从开始向后读,写和追加的时候指针会调到最后
w+,写读【可读,可写】,先清空数据,从开始向后读,写和追加的时候指针会调到最后
x+ ,写读【可读,可写】,同上
a+, 写读【可读,可写】,同上

"b"表示以字节的方式操作:

rb 或 r+b
wb 或 w+b
xb 或 w+b
ab 或 a+b
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型

二、操作文件

class TextIOWrapper(_TextIOBase):
    """
    Character and line based layer over a BufferedIOBase object, buffer.
    
    encoding gives the name of the encoding that the stream will be
    decoded or encoded with. It defaults to locale.getpreferredencoding(False).
    
    errors determines the strictness of encoding and decoding (see
    help(codecs.Codec) or the documentation for codecs.register) and
    defaults to "strict".
    
    newline controls how line endings are handled. It can be None, '',
    '\n', '\r', and '\r\n'.  It works as follows:
    
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
      these are translated into '\n' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
    
    * On output, if newline is None, any '\n' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '\n', no translation takes place. If newline is any
      of the other legal values, any '\n' characters written are translated
      to the given string.
    
    If line_buffering is True, a call to flush is implied when a call to
    write contains a newline character.
    """
    def close(self, *args, **kwargs): 
        """关闭文件"""      
        pass

    def detach(self, *args, **kwargs): 
        pass

    def fileno(self, *args, **kwargs): 
        """文件描述符"""  
        pass

    def flush(self, *args, **kwargs): 
        """把缓冲区的内容写入硬盘"""
        pass

    def isatty(self, *args, **kwargs): 
        pass

    def read(self, *args, **kwargs): 
        """读取指定字节数据"""
        pass

    def readable(self, *args, **kwargs): 
        pass

    def readline(self, *args, **kwargs): 
        """只读取一行数据"""
        pass

    def seek(self, *args, **kwargs): 
        """
        指定文件中指针位置
        f.seek(0) 把指针归零
        """
        pass

    def seekable(self, *args, **kwargs): 
        pass

    def tell(self, *args, **kwargs): 
        """获取当前指针位置"""
        pass

    def truncate(self, *args, **kwargs): 
        pass

    def writable(self, *args, **kwargs): 
        pass

    def write(self, *args, **kwargs): 
        pass

    def __getstate__(self, *args, **kwargs): 
        pass

    def __init__(self, *args, **kwargs): 
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): 
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __next__(self, *args, **kwargs): 
        """ Implement next(self). """
        pass

    def __repr__(self, *args, **kwargs): 
        """ Return repr(self). """
        pass

三、关闭文件

使用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()方法。

四、代码块

创建代码块能实现的功能:

1、每次操作完后不用再次手动关闭

2、能同时打开两个文件(2.7之后才有的功能)

批量操作


with open('1.txt', 'r') as f1, open('2.txt','r') as f2:
    pass

模拟文件复制的过程(针对大文件比较有效)


with open('1.txt', 'r') as f1, open('2.txt','w') as f2:
    for line in f1:         #读取原文件内容,然后一行一行写到新文件中
        f2.write(line)
posted @ 2016-10-16 18:07  morra  阅读(240)  评论(0编辑  收藏  举报