zoukankan      html  css  js  c++  java
  • python's eighth day for me

    f : 变量,f_obj, file, f_handler,...文件句柄。

    open : windows 的系统功能。

    windows 默认编码方式:gbk。   Linux 默认编码方式:utf - 8.

    f.close()   关闭文件。

    f = open(r'F:顾清秋.txt',encoding='utf-8',mode='r')  
    content = f.read()                                 
    print(content)                                     
    f.close()                                          

    文件执行动作(打开方式):

        只读,只写,追加,读写,写读....

        r  只读

        1.文件以什么编码方式存储的,就以什么编码方式打开,编码不一致时会报错。

        2.文件路径: 

            绝对路径:从根目录开始,一级一级查找到文件。F:顾清秋.txt

            相对路径:从同一个文件夹下,直接写文件名即可。

        rb  非文字类文件时,用rb.  (图片,视频等...)

        1.全部都出来 f.read()

        2.一行一行的读。

    f = open('log',encoding='utf-8')         
    print(f.readline())                      
    print(f.readline())                      
    f.close()                                
                                             
    # 顾清秋                                    
    #                #  自动换行                  
    # 顾清秋                                    

        3.将原文件的每一行作为一个列表的元素。

    f = open('log',encoding = 'utf-8')    
    print(f.readlines())                  
    f.close()                             
                                          
     # ['顾清秋
    ', '顾清秋
    ', '顾清秋']          

        4.读取一部分 read(n)  

        在 r 模式下,read(n)按照字符去读取。

    f = open('log1',encoding='utf-8')   
    print(f.read(2))                    
    f.close()                           
                                        
    # qw                                

        在 rb 模式下, read(n) 按照字节去读取。 

    f = open('log1',mode='rb')  
    content = f.read(2)         
    print(content)              
    f.close()                   
                                
    # b'qw'                             

         5. 循环读取

    f = open('log',encoding='utf-8')
    for i in f:
        print(i.strip())
    f.close()
    
    # 顾清秋
    # 顾清秋
    # 顾清秋

       w  只写:

        没有文件,创建一个文件写入内容。

       有文件,将原文件内容清空,再写入内容。

    f = open('log',encoding='utf-8',mode='w')
    f.write('懒笨呆')    #将懒笨呆写入到log文件中。
    f.close()

        wb  以bytes类型写入。   

    f = open('log',mode='wb')
    f.write('老男孩'.encode('utf-8'))   #将‘老男孩’编码成‘utf-8'写入。
    f.close()

        a  没有文件,创建一个文件追加内容。

         有文件,直接追加内容

    f =open('log2',encoding='utf-8',mode='a')
    f.write('顾清秋')     #将’顾清秋‘追加到'log2'文件中。
    f.close()

        r+  先读,后追加  一定要先读后写

    f = open('log1',encoding='utf-8',mode='r+')   #是先读,后追加!!!!
    content = f.read()
    print(content)
    f.write('123q')
    f.close()

        w+  先写后读

    f = open('log',encoding='utf-8',mode='w+')
    f.write('zhonguo')
    # print(f.tell())   #按字节去读光标位置
    f.seek(3)       #按照字节调整光标的位置
    print(f.read())
    f.close()

        truncate : 按字节对原文件截取   需要在 writing 下才可用此方法。

    f = open('log',encoding='utf-8',mode='a')   #追加模式下
    f.truncate(3)  #按字节对原文件截取
    f.close()

       with 功能一:可以不用写      close(),程序会自动close().

    with open('log',encoding='utf-8') as f:
        print(f.read())

       with 功能二:可以一次性操作多个文件句柄。

    with open('log',encoding='utf-8') as f1,        
            open('log1',encoding='utf-8') as f2:      #同时对'log','log2'进行只读操作。
        print(f1.read())
        print(f2.read())

    对文件进行操作的步骤:

        1,将原文件读取到内存。

       2,在内存中进行修改,形成新的内容。

       3,将新的字符串写入新文件。

       4,将原文件删除。

       5,将新文件重命名成原文件。

    import os    #引用os模块
    with open('log',encoding='utf-8') as f1,
        open('log3',encoding='utf-8',mode='w') as f2:
        content = f1.read()           #因为要全部读取才能替换,比较占空间,所以一般不用此方法。
        new_content = content.replace('顾清秋','懒笨呆')
        f2.write(new_content)
    os.remove('log')
    os.rename('log3','log')    
    import os
    with open('log',encoding='utf-8') as f1,
        open('log3',encoding='utf-8',mode='w') as f2:
        for i in f1:
            new_i = i.replace('懒笨呆','顾清秋')
            f2.write(new_i)
    os.remove('log')
    os.rename('log3','log')
  • 相关阅读:
    idea设置全局ignore
    win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistributable. Please ins
    win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistr
    kafka 删除 topic
    java编译中出现了Exception in thread “main" java.lang.UnsupportedClassVersionError
    Centos中使用yum安装java时,没有jps的问题的解决
    Spring 整合Junit
    Spring纯注解配置
    Spring 基于注解的 IOC 配置
    打印java系统的信息
  • 原文地址:https://www.cnblogs.com/stfei/p/8659807.html
Copyright © 2011-2022 走看看