1. 程式人生 > >課堂筆記:Python基礎-文件操作

課堂筆記:Python基礎-文件操作

def cas elf 擴展 中文 tell enum new span

對文件操作流程

  1. 打開文件,得到文件句柄並賦值給一個變量
  2. 通過句柄對文件進行操作
  3. 關閉文件

  現有文件如下:

  

昨夜寒蛩不住鳴。
驚回千裏夢,已三更。
起來獨自繞階行。
人悄悄,簾外月朧明。
白首為功名,舊山松竹老,阻歸程。
欲將心事付瑤琴。
知音少,弦斷有誰聽。

f = open(小重山) #打開文件
data=f.read()#獲取文件內容
f.close() #關閉文件

註意 :在Windows系統中,hello文件是utf8保存的,打開文件時open函數是通過操作系統打開的文件,而win操作系統

默認的是gbk編碼,所以直接打開會亂碼,需要f=open(‘hello‘

,encoding=‘utf8‘),hello文件如果是gbk保存的,則直接打開即可。

文件打開模式

========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    r       讀文件
    w       首先打開文件時,會清空原文件內容,再執行寫操作
    x       創建一個新文件並打開它寫入
    
a 打開寫入,如果存在,則附加到文件的末尾 b 二進制模式 t 文字模式(默認) + 打開一個磁盤文件進行更新(讀寫)例如:‘r+‘,‘w+‘,‘a+‘ U 通用換行模式(不推薦使用) ========= ===============================================================

三種最基本的模式:

# f = open(‘小重山2‘,‘w‘) #打開文件
# f = open(‘小重山2‘,‘a‘) #打開文件
# f.write(‘莫等閑1\n‘)
# f.write(‘白了少年頭2\n‘) # f.write(‘空悲切!3‘)

文件具體操作:

技術分享
def read(self, size=-1): # known case of _io.FileIO.read
        """
        註意,不一定能全讀回來
        Read at most size bytes, returned as bytes.

        Only makes one system call, so less data may be returned than requested.
        In non-blocking mode, returns None if no data is available.
        Return an empty bytes object at EOF.
        """
        return ""

def readline(self, *args, **kwargs):
        pass

def readlines(self, *args, **kwargs):
        pass


def tell(self, *args, **kwargs): # real signature unknown
        """
        Current file position.

        Can raise OSError for non seekable files.
        """
        pass

def seek(self, *args, **kwargs): # real signature unknown
        """
        Move to new file position and return the file position.

        Argument offset is a byte count.  Optional argument whence defaults to
        SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
        are SEEK_CUR or 1 (move relative to current position, positive or negative),
        and SEEK_END or 2 (move relative to end of file, usually negative, although
        many platforms allow seeking beyond the end of a file).

        Note that not all file objects are seekable.
        """
        pass

def write(self, *args, **kwargs): # real signature unknown
        """
        Write bytes b to file, return number written.

        Only makes one system call, so not all of the data may be written.
        The number of bytes actually written is returned.  In non-blocking mode,
        returns None if the write would block.
        """
        pass

def flush(self, *args, **kwargs):
        pass


def truncate(self, *args, **kwargs): # real signature unknown
        """
        Truncate the file to at most size bytes and return the truncated size.

        Size defaults to the current file position, as returned by tell().
        The current file position is changed to the value of size.
        """
        pass


def close(self): # real signature unknown; restored from __doc__
            """
            Close the file.

            A closed file cannot be used for further I/O operations.  close() may be
            called more than once without error.
            """
            pass
##############################################################less usefull
    def fileno(self, *args, **kwargs): # real signature unknown
            """ Return the underlying file descriptor (an integer). """
            pass

    def isatty(self, *args, **kwargs): # real signature unknown
        """ True if the file is connected to a TTY device. """
        pass

    def readable(self, *args, **kwargs): # real signature unknown
        """ True if file was opened in a read mode. """
        pass

    def readall(self, *args, **kwargs): # real signature unknown
        """
        Read all data from the file, returned as bytes.

        In non-blocking mode, returns as much as is immediately available,
        or None if no data is available.  Return an empty bytes object at EOF.
        """
        pass

    def seekable(self, *args, **kwargs): # real signature unknown
        """ True if file supports random-access. """
        pass


    def writable(self, *args, **kwargs): # real signature unknown
        """ True if file was opened in a write mode. """
        pass
View Code
f = open(小重山) #打開文件
# data1=f.read()#獲取文件內容
# data2=f.read()#獲取文件內容
#
# print(data1)
# print(‘...‘,data2)
# data=f.read(5)#獲取文件內容
 
# data=f.readline()
# data=f.readline()
# print(f.__iter__().__next__())
# for i in range(5):
#     print(f.readline())
 
# data=f.readlines()
 
# for line in f.readlines():
#     print(line)
 
 
# 問題來了:打印所有行,另外第3行後面加上:‘end 3‘
# for index,line in enumerate(f.readlines()):
#     if index==2:
#         line=‘‘.join([line.strip(),‘end 3‘])
#     print(line.strip())
 
#切記:以後我們一定都用下面這種
# count=0
# for line in f:
#     if count==3:
#         line=‘‘.join([line.strip(),‘end 3‘])
#     print(line.strip())
#     count+=1
 
# print(f.tell())
# print(f.readline())
# print(f.tell())#tell對於英文字符就是占一個,中文字符占三個,區分與read()的不同.
# print(f.read(5))#一個中文占三個字符
# print(f.tell())
# f.seek(0)
# print(f.read(6))#read後不管是中文字符還是英文字符,都統一算一個單位,read(6),此刻就讀了6個中文字符
 
#terminal上操作:
f = open(小重山2,w)
# f.write(‘hello \n‘)
# f.flush()
# f.write(‘world‘)
 
# 應用:進度條
# import time,sys
# for i in range(30):
#     sys.stdout.write("*")
#     # sys.stdout.flush()
#     time.sleep(0.1)
 
 
# f = open(‘小重山2‘,‘w‘)
# f.truncate()#全部截斷
# f.truncate(5)#全部截斷
 
 
# print(f.isatty())
# print(f.seekable())
# print(f.readable())
 
f.close() #關閉文件

繼續擴展文件模式:

# f = open(‘小重山2‘,‘w‘) #打開文件
# f = open(‘小重山2‘,‘a‘) #打開文件
# f.write(‘莫等閑1\n‘)
# f.write(‘白了少年頭2\n‘)
# f.write(‘空悲切!3‘)
 
 
# f.close()
 
#r+,w+模式
# f = open(‘小重山2‘,‘r+‘) #以讀寫模式打開文件
# print(f.read(5))#可讀
# f.write(‘hello‘)
# print(‘------‘)
# print(f.read())
 
 
# f = open(‘小重山2‘,‘w+‘) #以寫讀模式打開文件
# print(f.read(5))#什麽都沒有,因為先格式化了文本
# f.write(‘hello alex‘)
# print(f.read())#還是read不到
# f.seek(0)
# print(f.read())
 
#w+與a+的區別在於是否在開始覆蓋整個文件
 
 
# ok,重點來了,我要給文本第三行後面加一行內容:‘hello 嶽飛!‘
# 有同學說,前面不是做過修改了嗎? 大哥,剛才是修改內容後print,現在是對文件進行修改!!!
# f = open(‘小重山2‘,‘r+‘) #以寫讀模式打開文件
# f.readline()
# f.readline()
# f.readline()
# print(f.tell())
# f.write(‘hello 嶽飛‘)
# f.close()
# 和想的不一樣,不管事!那涉及到文件修改怎麽辦呢?
 
# f_read = open(‘小重山‘,‘r‘) #以寫讀模式打開文件
# f_write = open(‘小重山_back‘,‘w‘) #以寫讀模式打開文件
 
# count=0
# for line in f_read:
    # if count==3:
    #     f_write.write(‘hello,嶽飛\n‘)
    #
    # else:
    #     f_write.write(line)
 
 
    # another way:
    # if count==3:
    #
    #     line=‘hello,嶽飛2\n‘
    # f_write.write(line)
    # count+=1
 
 
# #二進制模式
# f = open(‘小重山2‘,‘wb‘) #以二進制的形式讀文件
# # f = open(‘小重山2‘,‘wb‘) #以二進制的形式寫文件
# f.write(‘hello alvin!‘.encode())#b‘hello alvin!‘就是一個二進制格式的數據,只是為了觀看,沒有顯示成010101的形式

註意1: 無論是py2還是py3,在r+模式下都可以等量字節替換,但沒有任何意義的! 

註意2:有同學在這裏會用readlines得到內容列表,再通過索引對相應內容進行修改,最後將列表重新寫會該文件。

這種思路有一個很大的問題,數據若很大,你的內存會受不了的,而我們的方式則可以通過叠代器來優化這個過程。 

with語句

為了避免打開文件後忘記關閉,可以通過管理上下文,即:

with open(log,r) as f:
        pass


等同於
f = open(log,r)  #這種方法打開文件後,是需要關閉的,f.close.(),而with方法則不需要

如此方式,當with代碼塊執行完畢時,內部會自動關閉並釋放文件資源。

在Python 2.7 後,with又支持同時對多個文件的上下文進行管理,即:

with open(log1) as obj1, open(log2) as obj2:
    pass

課堂筆記:Python基礎-文件操作