zoukankan      html  css  js  c++  java
  • python文件处理

    1.读取文件

     1.1 普通的输出

    >>> t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'r')
    >>> t1.read()
    '# 学习python
    # 学习云计算
    # 学习linux
    # 学习mq
    # 学习缓存
    # 学习mysql
    # 学习docker
    # 学习jvm 优化
    # 
    >>> t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'r')
    >>> t1.read(1)  //指定索引输出
    '#'
    >>> t1.read(2)
    ''
    >>>

     1.2.按照行来输出  readline

    t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'r')
    for i in range(3):
        print(str(i)+":"+t1.readline())   //按照行来输出

    输出
    /usr/local/bin/python3.7 /Users/guanbinbin/Downloads/workspace/python-learn-exercise/day21/fileTest.py
    0:# 学习python
    
    1:# 学习云计算
    
    2:# 学习linux
    1.3 reealines
    # 使用releadlines函数
    t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'r') print(t1.readlines()) #遍历输出 l = list(open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt", 'r')) print(l)
    t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'r')
    
    
    

     #使用for循环输出

      m = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/day21/test1.txt", 'r')

    for line in m.readlines():
    print("***"+line)
    m.close()
     
    open函数中模式参数中的常用值
    '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)

    注意:
    open函数的第三个参数控制文件的缓冲。如果参数是0(false),I/O(输入/输出)就是无缓冲的(所有的读写操作都是直接针对硬盘);
    如果参数是1(true),I/O(输入/输出)就是有缓冲的(意味着Python使用内存来代替硬盘,让程序更快,只有使用flush或者close时才会更新硬盘)

    2.写入文件

    2.1 seek函数,指定读写得位置;默认是0

    >>> f=open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/day21/test.txt",'w')
    >>> f.write("123456788")
    9
    >>> f.seek(5)
    5
    >>> f.write("Hello World!")
    12
    >>> f.close()
    >>> f=open(r"/Users/guanbinbin/Downloads/workspace/python-learn-exercise/day21/test.txt")
    >>> f.read()
    '12345Hello World!'

    2.2两种关闭流方式

    #1.第一种使用try/finally最终关闭文件
    t = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'w') try: t.write("Hello, ") t.write("World!") finally: t.close() print(t)
    #2.第二种,使用with操作,会在操作完,自动关闭流 with open(
    "/Users/guanbinbin/Downloads/workspace/python-learn-exercise/mysql.txt",'w') as w: w.write("Hello, ") w.write("World!")

    2.3按行写入文件

     文件test1.txt

    this
    is no
    money

    按行写入

    >>> t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/day21/test1.txt",'r')
    >>> lines=t1.readlines()
    >>> t1.close()
    >>> lines[1]="isn't a 
    "
    >>> t1 = open("/Users/guanbinbin/Downloads/workspace/python-learn-exercise/day21/test1.txt",'w')
    >>> t1.writelines(lines)
    >>> t.close()

    输出:

    this
    isn't a 
    money

    为什么要关闭流,因为写入时是暂存在内存中的,未写入磁盘中,使用close或者flush才会更新硬盘的数据;

    写入需要关闭流操作,读取没有写入内存的操作,可以不close操作,是没有错的,但是会占用系统的可打开文件句柄数。

    3.换行符:

    Unix系统里,每行结尾只有“<换行>”,即"
    ";                         使用nodepat ++ 打开的话行尾显示的是LF
    Mac系统里,每行结尾是“<回车>”,即"
    ";
    Windows系统里面,每行结尾是“<换行><回车 >”,即“
    
    ”。    使用nodepat ++ 打开的话行尾显示的是CR LF

    文件读取分为文本模式和二进制模式:

    python是跨平台语言,如果是文本模式解析文件时,会自动把换行符转化为当前操作系统的换行符,但是这样会破坏二进制得数据;如果不想破坏二进制数据的话,就以二进制格式读取,这样python不会进行转化,从而保证了二进制数据的完整性。

  • 相关阅读:
    C++模板总结
    Service介绍(MediaPlayer应用)
    Java创建WebService服务及客户端实现
    Tomcat服务器常用配置和HTTP简介
    JavaWeb学习篇之----HTTP协议详解
    JSP/Servlet(一)
    servlet+jsp+java实现Web 应用
    MySQL多表查询
    Java XML解析工具 dom4j介绍及使用实例
    java socket编程
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/12641689.html
Copyright © 2011-2022 走看看