zoukankan      html  css  js  c++  java
  • python之文件操作

    python 文件操作
    正常的文件操作都分三步走:
    打开文件
    操作文件
    关闭文件

    语法:文件句柄 = open(文件名,模式)
    r,只读模式(默认)。
    w,只写模式。【不可读;不存在则创建;存在则删除内容;】
    a,追加模式。【可读;不存在则创建;存在则只追加内容;】

    "+" 表示可以同时读写某个文件
    "U"表示在读取时,可以将 自动转换成 (注意:只能与 r 或 r+ 模式同使用)
    "b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
    r+,可读写文件。【可读;可写;可追加】
    w+,先写再读,与seek配合使用
    a+,同a

    几个常用方法
    read() #读取文件所有内容并返回字符串
    readline() #读取一行
    next() #读取下一行
    readlines() #读取所有内容,并返回列表(一行为列表的一个元素值)
    write() #写一行
    writelines(list) #写多行,参数为列表
    seek() #句柄指针操作
    tell() #当前句柄指针位置
    truncate() #截取文件句柄头到当前位置的字符串,返回None
    xreadlines() # 一行一行读,3.x版本已放弃此写法

    示例代码
    #代码示例
    #打开一个文件并写入
    fh = open('tfile','wb+')
    #文件句柄所在位置
    fh.tell()
    #写入一行
    fh.write('Life is like a roller coaster,live it,be happy,enjoy life.
    ')
    #写入多行,参数为列表list
    fh.writelines(["The best way to make your dreams come true is to wake up.
    ","If you're not making mistakes,you're not trying hard enough."])
    #返回文件句柄头
    fh.seek(0)
    #读一行
    fh.readline()
    #读所有行,返回列表list
    fh.readlines()
    #返回文件句柄头
    fh.seek(0)
    #读取全部内容,返回字符串
    fh.read()
    #返回文件句柄头
    fh.seek(0)
    fh.readline()
    #读取当前位置
    fh.tell()
    #截取文件句柄头到当前位置的字符串
    fh.truncate()
    fh.seek(0)
    fh.read()
    fh.writelines(["The best way to make your dreams come true is to wake up.
    ","If you're not making mistakes,you're not trying hard enough."])
    #读一行
    fh.seek(0)
    fh.next()
    #关闭文件句柄
    fh.close()

    result:

     1 >>> #代码示例
     2 ... #打开一个文件并写入
     3 ... fh = open('tfile','wb+')
     4 >>> #文件句柄所在位置
     5 ... fh.tell()
     6 0
     7 >>> #写入一行
     8 ... fh.write('Life is like a roller coaster,live it,be happy,enjoy life.
    ')
     9 >>> #写入多行,参数为列表list
    10 ... fh.writelines(["The best way to make your dreams come true is to wake up.
    ","If you're not making mistakes,you're not trying hard enough."])
    11 >>> #返回文件句柄头
    12 ... fh.seek(0)
    13 >>> #读一行
    14 ... fh.readline()
    15 'Life is like a roller coaster,live it,be happy,enjoy life.
    '
    16 >>> #读所有行,返回列表list
    17 ... fh.readlines()
    18 ['The best way to make your dreams come true is to wake up.
    ', "If you're not making mistakes,you're not trying hard enough."]
    19 >>> #返回文件句柄头
    20 ... fh.seek(0)
    21 >>> #读取全部内容,返回字符串
    22 ... fh.read()
    23 "Life is like a roller coaster,live it,be happy,enjoy life.
    The best way to make your dreams come true is to wake up.
    If you're not making mistakes,you're not trying hard enough."
    24 >>> #返回文件句柄头
    25 ... fh.seek(0)
    26 >>> fh.readline()
    27 'Life is like a roller coaster,live it,be happy,enjoy life.
    '
    28 >>> #读取当前位置
    29 ... fh.tell()
    30 59
    31 >>> #截取文件句柄头到当前位置的字符串
    32 ... fh.truncate()
    33 >>> fh.seek(0)
    34 >>> fh.read()
    35 'Life is like a roller coaster,live it,be happy,enjoy life.
    '
    36 >>> #读一行
    37 ... fh.seek(0)
    38 >>> fh.next()
    39 'Life is like a roller coaster,live it,be happy,enjoy life.
    '
    40 >>> #关闭文件句柄
    41 ... 

    当文件非常大时,要一行一行的读文件,要用for 循环,例子如下

    fh = open('tfile')
    for line in fh:
        print line 
    fh.close()

    有时我们常常忘记关闭文件句柄,如果不想多写一个关闭文件句柄的代码,可以使用with上下文操作(支持同时操作多个文件)

    语法:

    with open('tfile1') as fh1,open('tfile2') as fh2:
        ....
  • 相关阅读:
    爬取豆瓣电影
    post get 请求 headers注释哪些
    matlab 子图像subplot
    post请求get请求
    UA伪装
    urllib.request encode()/decode()
    urllib.parse quote/unquate/urlencode
    python 爬取图片
    二叉树满二叉树完全二叉树
    Linux | 性能分析系列学习 (1)
  • 原文地址:https://www.cnblogs.com/benric/p/5070079.html
Copyright © 2011-2022 走看看