Day 07 文件的相关操作

文件初始:

  文件的三要素:

 path:文件的路径

 mode:r w r+ w+ a

 encoding: 编码方式

#  打开一个文件的方法

f1 = open('e:\echo.txt', encoding='utf-8',mode='r')  # mode = 'r'可以省略
r1 = f1.read()
print(r1)
f1.close()


# 方法二,可以不接 f1.close()
with open('e:\echo.txt',encoding='utf-8', mode='r') as f1:
    r1 = f1.read()
    print(r1)

 打开文件可能会出现的集中报错

报错原因:
1,路径错误。 \与后面的那个字符具有了特殊意义。(e:\new.txt \n表示换行符)
解决方式:
r'd:\美女护士空姐联系方式.txt' 在路径最前面+ r
'd:\\美女护士空姐联系方式.txt' 第一个\对第二个进行转义。
2,Unicodedecodeerror: 编码问题。
encoding='utf-8' 打开文件的编码与文件存储时的编码不一致。

3, encoding 只是声明了此文件的需要用什么编码本编码解码而已。

4,路径:
绝对路径:从磁盘(根目录)开始,直到找到你的文件。
相对路径:当前路径(当前文件夹)找到的文件。


f1 = open('e:\echo.txt', encoding='utf-8',mode='r')
f1 f file file_handler ,f_h.... 文件句柄
open() 内置函数 这个函数实际上是调用的操作系统的对文件操作的功能,
windows:默认的编码方式gbk.
linux: 默认的编码方式utf-8.
IOS:默认的编码方式utf-8.
接下来你对文件进行的任何操作,都需借助文件句柄操作。
  1, 打开文件产生文件句柄(path, encoding mode)。
  2,对文件句柄进行操作。
  3,关闭文件句柄。
f1.close()   关闭文件,清除内存

r下的五种读取方式:
1、read()全部读取出来
2、read(n)读n个字符
  在 r 模式下按照字符读取
  在 rb 模式下按照字节读取
3、readline读取一行
4、readlines读取多行
5、for遍历
with open('file', encoding='utf-8') as f1:
    # r1 = f1.read()
    # # print(r1)

    # r1 = f1.read(3)  #  三个字符
    # print(r1)

    # r1 = f1.readline()
    # print(r1)           # 打印第一行
    # print(f1.readline())    #打印第二行
    # print(f1.readline())    #打印第三行

    # r1 = f1.readlines()
    # print(r1)     #   ['你好,世界!\n', 'hello wrod!\n', '欢迎来到python的世界']

    # for i in f1:
    #     print(i)  # for 遍历

rb模式
b模式操作的文件是非文字类的文件:图片,视频,音频等等
read() read(n) readline() readlines() for 循环
f = open('file1',mode='rb')
print(f.read())
f.close()
f = open('file1',mode='rb')
print(f.read(3)) # 按照字节
f.close()
with open('file', mode='rb') as f1:   # 不加encoding='utf-8',加了会报错
    r1 = f1.read()      # b'\xe4\xbd\xa0\xe5\xa5\xbd\r\n'
    r1 = f1.read(3)
    r1 = f1.readline()
    r1 = f1.readlines()
    print(r1)
    for i in f1:
        print(i)

r+ 模式   ==(r+w,可读,可写)

with open('file', encoding='utf-8', mode='r+') as f1:
    f1.write('你猜')    #这行放前面,会替换掉file里面,开始的两个字符
    r1 = f1.read()
    print(r1)       #  先读后写,不会把刚写入的内容读取出来,除非改变光标的位置

实现读取写入后的内容

with open('file', encoding='utf-8', mode='r+') as f1:
    f1.seek(0, 2)      #将光标调制file结尾,在结尾添加,不会覆盖文件
    f1.write('你猜hello')
    f1.seek(0)         #将光标调制file开始位置
    print(f1.read())

注意:在 r+ 模式下,应该先读,后写入。

w 模式 

没有文件创建文件写入,有文件清空原文件内容写入新内容。
w模式 必须是以字符串的内容写入
#先清空源文件,在将hello输入到源文件

with open('file',encoding='utf-8',mode='w') as f1:
    f1.write('hello')

用wb模式,完成对图片的复制

with open('timg.jpg', mode='rb') as f1:
    new_jpg = f1.read()
with open('timg3.jpg', mode='wb') as f2:
    f2.write(new_jpg)

W+ : 写读模式

with open('file', encoding='utf-8',mode='w+') as f1:
    f1.write('666')     # 先清空,后写入
    f1.seek(0)           # 将光标至开始位置
    print(f1.read())    

文件的追加   a

没有文件创建文件追加内容,有文件在原文件的末尾追加新内容
with open('file', encoding='utf-8', mode='a') as f1:
    f1.write('你好')

文件其他操作

文件所有操作
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): # real signature unknown
        关闭文件
        pass

    def fileno(self, *args, **kwargs): # real signature unknown
        文件描述符  
        pass

    def flush(self, *args, **kwargs): # real signature unknown
        刷新文件内部缓冲区
        pass

    def isatty(self, *args, **kwargs): # real signature unknown
        判断文件是否是同意tty设备
        pass

    def read(self, *args, **kwargs): # real signature unknown
        读取指定字节数据
        pass

    def readable(self, *args, **kwargs): # real signature unknown
        是否可读
        pass

    def readline(self, *args, **kwargs): # real signature unknown
        仅读取一行数据
        pass

    def seek(self, *args, **kwargs): # real signature unknown
        指定文件中指针位置
        pass

    def seekable(self, *args, **kwargs): # real signature unknown
        指针是否可操作
        pass

    def tell(self, *args, **kwargs): # real signature unknown
        获取指针位置
        pass

    def truncate(self, *args, **kwargs): # real signature unknown
        截断数据,仅保留指定之前数据
        pass

    def writable(self, *args, **kwargs): # real signature unknown
        是否可写
        pass

    def write(self, *args, **kwargs): # real signature unknown
        写内容
        pass

    def __getstate__(self, *args, **kwargs): # real signature unknown
        pass

    def __init__(self, *args, **kwargs): # real signature unknown
        pass

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

    def __next__(self, *args, **kwargs): # real signature unknown
        """ Implement next(self). """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    buffer = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    closed = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    encoding = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    errors = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    name = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    newlines = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
View Code

重点操作强调

flush  强制保存

with open('file',encoding='utf-8',mode='r') as f1:
    f1.write('你好')
    f1.flush()  #强制保存,相当于ctrl+s

readable writeable 判断一个文件句柄是否可读,可写

f1 = open('file', encoding='utf-8', mode='w')
print(f1.readable())   # True

f1 = open('file', encoding='utf-8', mode='w')
print(f1.readable())   # False 在写情况下,file不可读

with open('file', encoding='utf-8', mode='r') as f1:
    print(f1.writable())  # False 在读模式下不可写

with open('file', mode='w') as f1:
    print(f1.writable())  # True
seek tell 按照字节去调整读光标位置
f1 = open('file', encoding='utf-8', mode='r')
f1.seek(0, 2)
print(f1.tell())  # 显示光标的末尾(最后一个字节位置,如果有换行,要算两个字节“\n”)
print(f1.read())

 truncate()

只能在可写的模式下 截取原文件。只能从头截取,不能调整光标截取一部分。

不能在w模式下使用truncate
f1 = open('file', encoding='utf-8', mode='r+')
f1.truncate(3)   # 以字节为单位截取,弱是中文则必须以三个字节为一个单位截取

文件的改:

五个步骤

1、以读的模式打开原文件,产生文件句柄f1

2、以写模式打开新文件,产生文件句柄f2

3、读取源文件,将原文件的内容改写成新内容写入新文件

4、删除源文件

5、将新文件重命名成源文件

# 修改文件的五个步骤
import os
with open('log', encoding='utf-8', mode='r') as f1:            # 打开原文件log
    with open('log.bak', encoding='utf-8', mode='w') as f2:   # 打开新文件log.bak
        new_file = f1.read()    # read会读取所有文件,占内存,建议用for
        f2.write(new_file.upper())        # 将修改的内容写入新文件
os.remove('log')                               # 删除文件
os.rename('log.bak', 'log')                 # 修改名称

升级版

import os
with open('log', encoding='utf-8', mode='r') as f1, \
     open('log.bak', encoding='utf-8', mode='w') as f2:
        for i in f1:
            new_file = i.replace('ALEX', 'Echo')
            f2.write(new_file)
os.remove('log')
os.rename('log.bak', 'log')
 



























猜你喜欢

转载自www.cnblogs.com/echo2019/p/10186801.html