Python学习基础之-----------文件

版权声明:本博客内原创文章禁止转载 https://blog.csdn.net/qq_32466233/article/details/82261169

python打开文件主要采用open这个函数。open函数的文档介绍如下:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

open函数的参数介绍如下:

 ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)
    ========= ===============================================================

文件对象的使用方法:

f.close() #关闭文件
f.read(size=-1) #从文件中读取size个字符,当未给定size或者给定字符为负的时候,读取剩余的所有字符
f.readline()  # 以写入模式打开,如果文件存在,则在末尾追加写入
f.write(str) #将字符串str写入文件
f.writeline(seq) #将字符串序列写入文件seq,seq是一个返回字符串的可迭代对象
f.tell() #返回当前在文件中的位置 

猜你喜欢

转载自blog.csdn.net/qq_32466233/article/details/82261169