分享興趣,傳播快樂,增長見聞,留下美好!
親愛的您,這裡是LearningYard學苑。今天小編為大家帶來“學習知多少(python篇):檔案方法”,歡迎您的通路。
Share interests, spread happiness, increase knowledge, and leave a good legacy!
Dear you, this is The LearningYard Academy. Today Xiaobian brings you "Learn how much to know (python):File method”, welcome your visit.
在日常生活和工作中,我們常常需要多次使用一個檔案,避免繁瑣,我們可以借助python檔案操作功能實作簡化。檔案操作的作⽤就是把⼀些内容(資料)存儲存放起來,可以讓程式下⼀次執⾏的時候直接使⽤,⽽不必重新制作⼀份,省時省⼒。Python中有幾個内置子產品和方法來處理檔案,而使用Python對檔案進行讀和寫是十分簡單的。
In daily life and work, we often need to use a file multiple times, to avoid tediousness, we can use the Python file operation function to achieve simplification. The function of file operation is to store some content (data), so that the program can be used directly the next time it is executed, without having to make a new copy, saving time and effort. Python has several built-in modules and methods for working with files, and reading and writing files using Python is very simple.
1、open() 方法
Python open() 方法用于打開一個檔案,并傳回檔案對象。在對檔案進行處理過程都需要使用到這個函數,如果該檔案無法被打開,會抛出異常。
1. Open() method
The Python open() method is used to open a file and return a file object. This function is required to process the file, and if the file cannot be opened, an exception will be thrown.
注意:使用 open() 方法一定要保證關閉檔案對象,即調用 close() 方法。
Note: When using the open() method, be sure to close the file object, i.e. call the close() method.
open() 函數常用形式是接收兩個參數:檔案名(file)和模式(mode)。
The open() function usually takes two arguments: file and mode.
(1) mode 的分類
t:文本模式 (預設)
x:寫模式,建立一個檔案,如果該檔案已存在則會報錯
b:二進制模式
+:打開一個檔案進行更新(可讀可寫)
r:以隻讀方式打開檔案。檔案的指針将會放在檔案的開頭
w:打開一個檔案隻用于寫入。如果該檔案已存在則打開檔案,并從開頭開始編輯,即原有内容會被删除。如果該檔案不存在,建立新檔案
a:打開一個檔案用于追加。如果該檔案已存在,檔案指針将會放在檔案的結尾
ab:以二進制格式打開一個檔案用于追加。如果該檔案已存在,檔案指針将會放在檔案的結尾
ab+:以二進制格式打開一個檔案用于追加。如果該檔案已存在,檔案指針将會放在檔案的結尾
(1) Classification of modes
t: Text mode (default)
x: Write mode, create a new file, if the file already exists, an error will be reported
b: Binary mode
+: Open a file to update (readable and writable)
r: Open the file as read-only. The pointer to the file will be placed at the beginning of the file
w: Opens a file for writing only. If the file already exists, open the file and edit from the beginning, i.e. the original content is deleted. If the file does not exist, create a new file
a: Open a file for appending. If the file already exists, the file pointer is placed at the end of the file
ab: Opens a file in binary format for appending. If the file already exists, the file pointer is placed at the end of the file
ab+: Opens a file in binary format for appending. If the file already exists, the file pointer is placed at the end of the file
2、file 對象
file 對象使用 open 函數來建立,下面列出了 file 對象常用的函數:
2. File object
The file object is created using the open function, and the functions commonly used by the file object are listed below:
file.close()
關閉檔案。關閉後檔案不能再進行讀寫操作。
file.flush()
重新整理檔案内部緩沖,直接把内部緩沖區的資料立刻寫入檔案, 而不是被動的等待輸出緩沖區寫入。
file.next()
Python 3 中的 File 對象不支援 next() 方法。
傳回檔案下一行。
file.read([size])
從檔案讀取指定的位元組數,如果未給定或為負則讀取所有。
file.readlines([sizeint])
讀取所有行并傳回清單,若給定sizeint>0,傳回總和大約為sizeint位元組的行, 實際讀取值可能比 sizeint 較大, 因為需要填充緩沖區。
file.tell()
傳回檔案目前位置。
file.write(str)
将字元串寫入檔案,傳回的是寫入的字元長度。
file.close()
Close the file. After closing, the file can no longer be read or written.
file.flush()
Flush the internal buffer of the file, and directly write the data of the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written.
file.next()
The File object in Python 3 does not support the next() method.
Returns the next line of the file.
file.read([size])
Reads the specified number of bytes from the file, or all if not given or negative.
file.readlines([sizeint])
Read all rows and return a list, if a sizeint is given >0, return rows with a sum of approximately sizeint bytes, the actual read value may be larger than sizeint because the buffer needs to be filled.
file.tell()
Returns the current location of the file.
file.write(str)
Writes a string to a file, returning the length of the characters written.
3、檔案和檔案夾操作
在Python中⽂件和⽂件夾的操作要借助os子產品⾥⾯的相關功能,具體步驟如下:
1、導入os子產品(import os)
2、使用os子產品裡的函數(os.函數名())
1)檔案重命名
os.rename(⽬标⽂件名, 新⽂件名)
2)删除檔案
os.remove(⽬标⽂件名)
3)建立檔案夾
os.mkdir(⽂件夾名字)
4)删除檔案夾
os.rmdir(⽂件夾名字)
5)擷取目前目錄
os.getcwd()
6)改變預設目錄
os.chdir(⽬錄)
7)擷取目錄清單
os.listdir(⽬錄)
3. File and folder operations
The operation of Python Chinese and folders should use the relevant functions in the os module, and the specific steps are as follows:
1. Import OS module
2. Use the function in the OS module (OS. function name ())
1) File renaming
os.rename (target filename, new filename)
2) Delete the file
os.remove (destination file name)
3) Create a folder
os.mkdir (folder name)
4) Delete the folder
os.rmdir (folder name)
5) Get the current directory
os.getcwd()
6) Change the default directory
os.chdir (directory)
7) Get a directory listing
os.listdir (directory)
今天的分享就到這裡了。
如果您對今天的文章有獨特的想法,
歡迎給我們留言,
讓我們相約明天。
祝您今天過得開心快樂!
That's all for today's sharing.
If you have a unique idea for today’s article,
please leave us a message,
and let us meet tomorrow.
I wish you a happy day !
本文由learningyard新學苑原創,如有侵權,請聯系我們!
翻譯來源于谷歌翻譯
部分來源于
百度文庫
清華大學出版 董付國《Python程式設計基礎》
編輯&排版|百味
稽核|闫慶紅