Python 檔案操作----大檔案處理

來源:互聯網
上載者:User

標籤:position   pen   檔案   utf-8   開啟   open   write   osi   取資料   

#coding=utf-8

#當讀取某個不知名的檔案時,切記不要用read(),否則一定會報異常

 

#1.擷取使用者名稱要複製的檔案

old_file_name = input("輸入要複製的檔案名稱:")

#2.開啟要複製的檔案

old_file = open(old_file_name,"r")

position = old_file_name.rfind(".")

new_file_name = old_file_name[:position] + "[複件]" + old_file_name[position:]

#3.建立一個檔案

new_file = open(new_file_name,"w")

#4.從舊檔案中讀取資料,並且寫入到新檔案

while True:

     content = old_file.read(1024)

     new_file.write(content)

#5. 關閉兩個檔案

old_file.close()

new_file.close()

 

Python 檔案操作----大檔案處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.