Python基礎10—I/O編程

來源:互聯網
上載者:User

標籤:緩衝   file   複製   src   檔案的   操作   off   stat   ali   

一、輸入輸出

  ①輸入:input()

  ②輸出:print()

1 name=input(‘請輸入名字‘)2 print(name)

二、檔案操作

  ①開啟檔案:檔案對象=open(檔案名稱,訪問模式,buffering)

    檔案名稱:指定要開啟的檔案,通常需要包含路徑,可以是絕對路徑也可以是相對路徑

    buffering:選擇性參數,指定檔案所採用的緩衝方式,buffering=0,不緩衝;buffering=1,緩衝一行;buffering>1,使用給定值作為緩衝區大小

    訪問模式:用於指定開啟檔案的模式,訪問模式的參數表如下

可取值 含義
r 以讀方式開啟
w 以寫的方式開啟,此時檔案內容會被前清空,如果檔案不存在則會建立檔案
a 以追加的模式開啟,從檔案末尾開始,必要時建立新檔案
r+、w+ 以讀寫入模式開啟
a+ 追加的讀寫入模式開啟
rb 以二進位讀模式開啟
wb 以二進位寫入模式開啟
rb+、wb+、ab+ 以二進位讀寫入模式開啟

  ②關閉檔案:檔案對象.close()

  ③讀取檔案:

    Ⅰ:str=檔案對象.read([b])

    選擇性參數[b]:指定讀取的位元組數,預設讀取全部

    Ⅱ:list=檔案對象.readlines()讀取的內容返回到字串列表中

    Ⅲ:str=檔案對象.readline()一次性讀取檔案中的所有行

    Ⅳ:使用in關鍵字:for line in 檔案對象:處理行資料line

 1 f=open(‘test.txt‘) 2 mystr=f.read() 3 f.close() 4 print(mystr) 5  6 f=open(‘test.txt‘) 7 while True: 8     chunk=f.read(10) 9     if not chunk:10         break11     print(chunk)12 f.close
 1 f=open(‘test.txt‘) 2 mylist=f.readlines() 3 print(mylist) 4 f.close() 5 f=open(‘test.txt‘) 6 while True: 7     chunk=f.readline() 8     if not chunk: 9         break10     print(chunk)11 f.close()

三、寫入檔案

  ①write():檔案對象.write(寫入的內容)

  ②追加寫入:開啟檔案時可以用a或a+作為參數調用open()然後再寫入

  ③writelines():檔案對象.(seq)  用於寫入字串序列,seq是個返回字串序列(列表、元組、集合等)

1 f=open(‘test.txt‘,‘a+‘)2 f.write(‘hello‘)3 f.close()4 f=open(‘test.txt‘)5 for line in f:6     print(line)7 f.close()
1 mylist=[‘hello Jack‘,‘hello Salar‘,‘hello coffee‘]2 f=open(‘test.txt‘,‘a+‘)3 f.writelines(mylist)4 f.close()5 f=open(‘test.txt‘)6 for line in f:7     print(line)8 f.close()

四、檔案指標

  ①擷取檔案指標位置:pos=檔案對象.tell()  返回的是一個整數,表示檔案指標的位置,開啟一個檔案時檔案指標的位置為0,讀寫檔案是,檔案指標的位置會移至讀寫的位置

  ②移動檔案指標:檔案對象.seek((offset,where))  

    offset:移動的位移量,單位為位元組,正數時向檔案尾方向移動,負數時向檔案頭方向移動

    where:指從哪裡開始移動,0為起始位置,等於1時為當前位置移動,等於2時從結束位置開始移動  

1 f=open(‘test.txt‘)2 print(f.tell())3 f.close()4 f=open(‘test.txt‘)5 f.seek(5,0)6 print(f.tell())7 f.close()

五、截斷檔案

  檔案對象.truncate([size])  從檔案頭開始截斷檔案,size為選擇性參數,size位元組後的檔案內容將會被截斷丟掉

1 f=open(‘test.txt‘,‘a+‘)2 f.truncate(5)3 f.close()4 f=open(‘test.txt‘)5 for line in f:6     print(line)7 f.close()

六、其他動作

  ①檔案屬性: 使用os模組的stat()函數一科擷取檔案的屬性,建立時間、修改時間、訪問時間、檔案大小等屬性(使用前要匯入模組:import os)

  ②複製檔案:使用shutil模組的copy()函數即可,函數原型為:copy(src,dst)表示為把源檔案src複製為dst

  ③移動檔案:使用shutil模組的move()函數即可,函數原型為:move(src,dst)

  ④重新命名檔案:使用os模組的rename()函數即可,函數原型為:os.rename(原檔案名稱,新檔案名稱)

  ⑤刪除檔案:使用os模組的remove()函數即可,函數原型為:os.remove(src)

1 import os2 import shutil3 fileStats=os.stat(‘test.txt‘)4 print(fileStats)5 6 shutil.copy(‘F:\\python\\book\\ch7\\test.txt‘,‘F:\\python\\book\\ch7\\test2.txt‘)7 shutil.move(‘F:\\python\\book\\ch7\\test2.txt‘,‘F:\\python\\book\\test2.txt‘)8 os.rename(‘F:\\python\\book\\test2.txt‘,‘F:\\python\\book\\test3.txt‘)9 os.remove(‘F:\\python\\book\\test3.txt‘)

七、目錄編程

  ①擷取目前的目錄:os模組的getcwd()函數

  ②擷取目錄內容:os模組的listdir()函數

  ③建立目錄:os模組的mkdir()函數

  ④刪除目錄:os模組的rmdir()函數

1 import os2 print(os.getcwd())3 print(os.listdir(os.getcwd()))4 os.mkdir(‘F:\\python\\book\ch7\\he‘)5 os.rmdir(‘F:\\python\\book\ch7\\he‘)

 

Python基礎10—I/O編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.