Python – File

測試環境為 CentOS 8 x86_64 (虛擬機)

參考文章 – https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-bf0648108581

Python 提供 檔案物件 file object 來存取檔案.

開啟檔案 open()

open(file, mode='模式')
  • r
    Read 唯讀模式.
  • w
    Write 寫入模式,當檔案不存在時新增檔案,檔案存在時覆寫原檔案.
  • a
    append 附加模式,新增內容到檔案的後面.

檔案範本

[root@localhost ~]# cat PythonSample.txt 
Python number one
Taiwan number one

直接進入 Python command 模式,來試試不同讀取檔案的方式.

[root@localhost ~]# python3
Python 3.6.8 (default, Mar 25 2022, 11:15:52) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
  • file.read()
    f1=open('PythonSample.txt','r')
    words = f1.read()
    type(words)
    f.close()
    

    執行結果

    >>> f1=open('PythonSample.txt','r')
    >>> words = f1.read()
    >>> print(words)
    Python number one
    Taiwan number one
    
    >>> type(words)
    <class 'str'>
    
    >>> f.close()
    
  • file.readline()
    每次讀一行.

    f1=open('PythonSample.txt','r')
    words = f1.readline()
    print(words)
    type(words)
    f.close()
    

    執行結果

    >>> f1=open('PythonSample.txt','r')
    >>> words = f1.readline()
    >>> print(words)
    Python number one
    
    >>> type(words)
    <class 'str'>
    
    >>> f.close()
    
  • file.readlines()
    讀取的資料存放成 list (串列).

    f1=open('PythonSample.txt','r')
    for lines in f1.readlines():
        print(lines)
    type(words)
    f.close()
    

    執行結果

    >>> f1=open('PythonSample.txt','r')
    >>> for lines in f1.readlines():
    ...     print(lines)
    ... 
    Python number one
    
    Taiwan number one
    
    >>> type(words)
    <class 'list'>
    >>> f.close()
    
  • file.write()
    寫入檔案.

    f = open('PythoneWrite.txt', 'w')
    f.write("Write file")
    f.close()
    

    執行結果

    >>> f = open('PythoneWrite.txt', 'w')
    >>> f.write("Write file")
    10
    >>> f.close()
    >>> 
    [2]+  已停止               python3
    [root@localhost ~]# cat PythoneWrite.txt
    Write file
    
  • file.writelines()
    寫入檔案.其資料格式須為 list (陣列).

    f = open('PythoneWrite.txt', 'w')
    data1 = ["Python write file\n","Sample here"]
    f.writelines(data1)
    f.close()
    

    執行結果

    >>> f = open('PythoneWrite.txt', 'w')
    >>> data1 = ["Python write file\n","Sample here"]
    >>> f.writelines(data1)
    >>> f.close()
    >>> 
    [3]+  已停止               python3
    [root@localhost ~]# cat PythoneWrite.txt
    Python write file
    Sample here
    
  • file.seek()
    有個指標來標記檔案目前在哪一行,指標在讀取所有資料後就會指向在檔案最後,再次去讀取檔案就讀不到資料(因為指標已經在最後了),須透過 seek()重新指定檔案的指標 (0 表示為檔案最前面).

    f1=open('PythonSample.txt','r')
    words = f1.read()
    print(words);
    
    words = f1.read()
    print(words);
    
    f1.seek(0)
    words = f1.read()
    print(words);
    

    執行結果

    >>> f1=open('PythonSample.txt','r')
    >>> words = f1.read()
    >>> print(words);
    Python number one
    Taiwan number one
    
    >>> words = f1.read()
    >>> print(words);
    
    >>> f1.seek(0)
    0
    >>> words = f1.read()
    >>> print(words);
    Python number one
    Taiwan number one
    
  • file.tell()
    指出目前檔案指標( 0 表示檔案的最前面)在哪裡.

    f1=open('PythonSample.txt','r')
    words = f1.readline(3)
    print(words);
    print(f1.tell());
    

    執行結果

    >>> f1=open('PythonSample.txt','r')
    >>> words = f1.readline(3)
    >>> print(words);
    Pyt
    >>> print(f1.tell());
    3
    
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料