Python文件處理


1. 文件的操作

1.1 打開文件

格式:

def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)

源碼:

 1 def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open
 2     """
 3  Open file and return a stream. Raise IOError upon failure.  4     
 5  file is either a text or byte string giving the name (and the path  6  if the file isn't in the current working directory) of the file to  7  be opened or an integer file descriptor of the file to be  8  wrapped. (If a file descriptor is given, it is closed when the  9  returned I/O object is closed, unless closefd is set to False.)  10     
 11  mode is an optional string that specifies the mode in which the file  12  is opened. It defaults to 'r' which means open for reading in text  13  mode. Other common values are 'w' for writing (truncating the file if  14  it already exists), 'x' for creating and writing to a new file, and  15  'a' for appending (which on some Unix systems, means that all writes  16  append to the end of the file regardless of the current seek position).  17  In text mode, if encoding is not specified the encoding used is platform  18  dependent: locale.getpreferredencoding(False) is called to get the  19  current locale encoding. (For reading and writing raw bytes use binary  20  mode and leave encoding unspecified.) The available modes are:  21     
 22  ========= ===============================================================  23  Character Meaning  24  --------- ---------------------------------------------------------------  25  'r' open for reading (default)  26  'w' open for writing, truncating the file first  27  'x' create a new file and open it for writing  28  'a' open for writing, appending to the end of the file if it exists  29  'b' binary mode  30  't' text mode (default)  31  '+' open a disk file for updating (reading and writing)  32  'U' universal newline mode (deprecated)  33  ========= ===============================================================  34     
 35  The default mode is 'rt' (open for reading text). For binary random  36  access, the mode 'w+b' opens and truncates the file to 0 bytes, while  37  'r+b' opens the file without truncation. The 'x' mode implies 'w' and  38  raises an `FileExistsError` if the file already exists.  39     
 40  Python distinguishes between files opened in binary and text modes,  41  even when the underlying operating system doesn't. Files opened in  42  binary mode (appending 'b' to the mode argument) return contents as  43  bytes objects without any decoding. In text mode (the default, or when  44  't' is appended to the mode argument), the contents of the file are  45  returned as strings, the bytes having been first decoded using a  46  platform-dependent encoding or using the specified encoding if given.  47     
 48  'U' mode is deprecated and will raise an exception in future versions  49  of Python. It has no effect in Python 3. Use newline to control  50  universal newlines mode.  51     
 52  buffering is an optional integer used to set the buffering policy.  53  Pass 0 to switch buffering off (only allowed in binary mode), 1 to select  54  line buffering (only usable in text mode), and an integer > 1 to indicate  55  the size of a fixed-size chunk buffer. When no buffering argument is  56  given, the default buffering policy works as follows:  57     
 58  * Binary files are buffered in fixed-size chunks; the size of the buffer  59  is chosen using a heuristic trying to determine the underlying device's  60  "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.  61  On many systems, the buffer will typically be 4096 or 8192 bytes long.  62     
 63  * "Interactive" text files (files for which isatty() returns True)  64  use line buffering. Other text files use the policy described above  65  for binary files.  66     
 67  encoding is the name of the encoding used to decode or encode the  68  file. This should only be used in text mode. The default encoding is  69  platform dependent, but any encoding supported by Python can be  70  passed. See the codecs module for the list of supported encodings.  71     
 72  errors is an optional string that specifies how encoding errors are to  73  be handled---this argument should not be used in binary mode. Pass  74  'strict' to raise a ValueError exception if there is an encoding error  75  (the default of None has the same effect), or pass 'ignore' to ignore  76  errors. (Note that ignoring encoding errors can lead to data loss.)  77  See the documentation for codecs.register or run 'help(codecs.Codec)'  78  for a list of the permitted encoding error strings.  79     
 80  newline controls how universal newlines works (it only applies to text  81  mode). It can be None, '', '\n', '\r', and '\r\n'. It works as  82  follows:  83     
 84  * On input, if newline is None, universal newlines mode is  85  enabled. Lines in the input can end in '\n', '\r', or '\r\n', and  86  these are translated into '\n' before being returned to the  87  caller. If it is '', universal newline mode is enabled, but line  88  endings are returned to the caller untranslated. If it has any of  89  the other legal values, input lines are only terminated by the given  90  string, and the line ending is returned to the caller untranslated.  91     
 92  * On output, if newline is None, any '\n' characters written are  93  translated to the system default line separator, os.linesep. If  94  newline is '' or '\n', no translation takes place. If newline is any  95  of the other legal values, any '\n' characters written are translated  96  to the given string.  97     
 98  If closefd is False, the underlying file descriptor will be kept open  99  when the file is closed. This does not work when a file name is given 100  and must be True in that case. 101     
102  A custom opener can be used by passing a callable as *opener*. The 103  underlying file descriptor for the file object is then obtained by 104  calling *opener* with (*file*, *flags*). *opener* must return an open 105  file descriptor (passing os.open as *opener* results in functionality 106  similar to passing None). 107     
108  open() returns a file object whose type depends on the mode, and 109  through which the standard file operations such as reading and writing 110  are performed. When open() is used to open a file in a text mode ('w', 111  'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open 112  a file in a binary mode, the returned class varies: in read binary 113  mode, it returns a BufferedReader; in write binary and append binary 114  modes, it returns a BufferedWriter, and in read/write mode, it returns 115  a BufferedRandom. 116     
117  It is also possible to use a string or bytearray as a file for both 118  reading and writing. For strings StringIO can be used like a file 119  opened in a text mode, and for bytes a BytesIO can be used like a file 120  opened in a binary mode. 121     """
122     pass
文件打開源碼
  • file:被打開的文件名稱。
  • mode:文件打開模式,默認模式為r。
  • buffering:設置緩存模式。0表示不緩存,1表示緩存,如果大於1則表示緩存區的大小,以字節為單位。
  • encoding:字符編碼。

 

文件打開模式

‘r’

以只讀方式打開文件,默認。

‘w’

以寫入方式打開文件。先刪除原文件內容,再重新寫入新內容。如果文件不存在,則創建。

‘x’

以寫入方式創建新文件並打開文件。

‘a’

以寫入的方式打開文件,在文件末尾追加新內容。如果文件不存在,則創建。

‘b’

以二進制模式打開文件,與r、w、a、+結合使用。對於圖片、視頻等必須用b模式打開。

‘t’

文本模式,默認。與r、w、a結合使用。

‘+’

打開文件,追加讀寫。

‘U’

universal newline mode (deprecated棄用)支持所有的換行符號。即"U"表示在讀取時,可以將 \r \n \r\n自動轉換成 \n (與 r 或r+ 模式同使用)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


語法:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

f = open('demo')
print(f)
f.close()
# <_io.TextIOWrapper name='demo' mode='r' encoding='UTF-8'>
#python3默認以可讀(r)、'utf-8'編碼方式打開文件

###===r模式打開文件===
f = open('demo.txt',mode='r',encoding='utf-8')  #文件句柄
data = f.read()
print(data)
f.close()


###===w模式打開文件===
#a.該文件存在
f = open('demo.txt',mode='w',encoding='utf-8')
# data = f.read()   #io.UnsupportedOperation: not readable,不可讀
f.close()
#發現使用w打開原有文件,盡管什么都不做,該文件內容已被清空(刪除)

###b.該文件不存在
f = open('demo1.txt',mode='w',encoding='utf-8')
f.close()
#不存在則創建該文件


###===a模式打開文件===
f = open('demo2.txt',mode='a',encoding='utf-8')
# data = f.read()   #io.UnsupportedOperation: not readable
f.write('這是一首詞牌名!')
f.close()
#該文件不存在則創建新文件並寫入內容,存在則在后面追加內容。

###===b模式打開文件===
# f = open('demo2.txt',mode='b',encoding='utf-8')
# #ValueError: binary mode doesn't take an encoding argument

# f = open('demo2.txt',mode='b')
#alueError: Must have exactly one of create/read/write/append mode and at most one plus

f = open('demo3.txt',mode='br')
data = f.read()
print(data)
f.close()

###===t模式打開文件===
# f = open('demo3.txt','t')
#ValueError: Must have exactly one of create/read/write/append mode and at most one plus

f = open('demo3.txt','tr')
data = f.read()
print(data)
f.close()

###===x模式打開文件===
# 如果文件存在則報錯:FileExistsError: [Errno 17] File exists: 'demo4.txt'
f = open('demo41.txt',mode='x',encoding='utf-8')
f.write('人生苦短,我用python')
f.close()

###===+模式打開文件===
# f = open('demo4.txt','+')
#ValueError: Must have exactly one of create/read/write/append mode and at most one plus

#+需要和a/r/w/x結合使用,並且添加追加功能,即寫文件不刪除原內容
f = open('demo4.txt','r+')
data = f.read()
print(data)
f.write("人生苦短,我用python")    #不刪除原有內容
f.close()

###===U模式打開文件===
f = open('demo5.txt','U')
# f.write('hello')    #io.UnsupportedOperation: not writable不可寫
print(f.readlines())    #['adcdefg\n', '    dsfsdfs\n', 'hijklmn']
f.close()

 

with...as...打開文件: 

  通過with打開文件,每次執行完畢之后會自動關閉該文件。

 

#打開一個文件
with open('demo','r',encoding='utf-8') as f:
    print(f.read())
#連續打開兩個文件
with open('demo1.txt','r',encoding='utf-8') as f1,open('demo2.txt','r',encoding='utf-8') as f2:
    print(f1.read(),f2.read())

 

 

1.2 文件讀取

  文件讀取有三種方式,read()、readline()、readlines()。

 1     def read(self, size=-1): # known case of _io.FileIO.read
 2         """
 3  Read at most size bytes, returned as bytes.  4         
 5  Only makes one system call, so less data may be returned than requested.  6  In non-blocking mode, returns None if no data is available.  7  Return an empty bytes object at EOF.  8         """
 9         return ""
10     
11     def readline(self, limit=-1): 12         """Read and return one line from the stream. 13 
14  :type limit: numbers.Integral 15  :rtype: unicode 16         """
17         pass
18 
19     def readlines(self, hint=-1): 20         """Read and return a list of lines from the stream. 21 
22  :type hint: numbers.Integral 23  :rtype: list[unicode] 24         """
25         return []
文件讀取源代碼

read():
  從文件中一次性讀取所有內容,該方法耗內存。

#Read at most size bytes, returned as bytes.
f = open('demo',mode='r',encoding='utf-8')
data = f.read()
print(data)
f.close()

f = open('demo.txt',mode='r',encoding='utf-8')
data = f.read(5)    #讀取文件前5個字節
print(data)
f.close()

 

readline():

  每次讀取文件中的一行,需要使用用真表達式循環讀取文件。當文件指針移動到末尾時,依然使用readline()讀取文件將出現錯誤。因此需要添加1個判斷語句,判斷指針是否移動到文件尾部,並終止循環。

f = open('demo',mode='r',encoding='utf-8')
while True:
    line = f.readline()
    if line:
        print(line.strip())
    else:
        print(line)     #空
        print(bool(line))   #False
        break

   可以加參數,參數為整數,表示每行讀幾個字節,知道行末尾。

f = open('demo',mode='r',encoding='utf-8')
while True:
    line = f.readline(8)
    if line:
        print(line.strip())
    else:
        break


'''
###################demo文件內容##############
佇倚危樓風細細。
望極春愁,黯黯生天際。
草色煙光殘照里。無言誰會憑闌意。

擬把疏狂圖一醉。
對酒當歌,強樂還無味。
衣帶漸寬終不悔。為伊消得人憔悴。
#########################################
運行結果:
佇倚危樓風細細。

望極春愁,黯黯生
天際。
草色煙光殘照里。
無言誰會憑闌意。


擬把疏狂圖一醉。

對酒當歌,強樂還
無味。
衣帶漸寬終不悔。
為伊消得人憔悴。
'''

 

readlines():

  表示多行讀取,需要通過循環訪問readlines()返回列表中的元素,該方法耗內存。

f = open('demo','r')
lines = f.readlines()
print(lines)
print(type(lines))    #返回的是一個列表<class 'list'>
for line in lines:
    print(line.strip())
f.close()

 

練習:讀取前5行內容

 1 #方法一:使用readlines一次性讀取
 2 f = open('demo',mode='r',encoding='utf-8')  3 data = f.readlines()  4 for index,line in enumerate(data):  5     if index==5:  6         break
 7     print(line)  8 f.close()  9 
10 #方法二:使用readline逐行讀取(推薦該方法,省內存)
11 f = open('demo',mode='r',encoding='utf-8') 12 for line in range(5): 13     print(f.readline())
View Code

 

1.3 文件的寫入

  文件的寫入可以使用write()、writelines()方法寫入。write()把字符串寫入文件,writelines()可以把列表中存儲的內容寫入文件。

  使用wiritelines()寫文件的速度更快,如果需要寫入文件的字符串較多,可以使用writelines。如果只需寫入少量字符串則可以使用write()

源碼:

    def write(self, s):
        """Write the unicode string s to the stream and return the number of
        characters written.

        :type b: unicode
        :rtype: int
        """
        return 0


    def writelines(self, lines):
        """Write a list of lines to the stream.

        :type lines: collections.Iterable[unicode]
        :rtype: None
        """
        pass

 

write():

f = open('demo','a+')
print(f.write('\n這是一首詞牌名'))
f.close()

 

 writelines():

l = ['a','b','c','1','2','3']
f = open('demo','a+')
f.writelines(l)     #寫入內容:abcabc123
f.close()

 

1.4 其他操作

    def close(self, *args, **kwargs): # real signature unknown
        """
        Flush and close the IO object.
        
        This method has no effect if the file is already closed.
        """
        pass
#刷新並關閉文件
f = open('a.txt','a+',encoding='utf-8')
f.write('hello')
f.close()
	
	

    def fileno(self, *args, **kwargs): # real signature unknown
        """
        Returns underlying file descriptor if one exists.
        
        OSError is raised if the IO object does not use a file descriptor.
        """
        pass
#文件描述符
f = open('a.txt','r',encoding='utf-8')
print(f.fileno())
f.close()



    def flush(self, *args, **kwargs): # real signature unknown
        """
        Flush write buffers, if applicable.
        
        This is not implemented for read-only and non-blocking streams.
        """
        pass
# 刷新文件內部緩沖區
f = open('a.txt','a+',encoding='utf-8')
print(f.fileno())
f.write("JJJJJJJJJJJJJJJJJJ")
f.flush()
input = input("wait for input:")
f.close()



    def isatty(self, *args, **kwargs): # real signature unknown
        """
        Return whether this is an 'interactive' stream.
        
        Return False if it can't be determined.
        """
        pass
#判斷文件是不是tty設備
f = open('a.txt','r',encoding='utf-8')
print(f.isatty())
f.close()



    def readable(self, *args, **kwargs): # real signature unknown
        """
        Return whether object was opened for reading.
        
        If False, read() will raise OSError.
        """
        pass
#判斷文件是否可讀
f = open('a.txt','w',encoding='utf-8')
print(f.readable()) #False,因為使用w模式不可讀
f.close()



    def seek(self, *args, **kwargs): # real signature unknown
        """
        Change stream position.
        
        Change the stream position to the given byte offset. The offset is
        interpreted relative to the position indicated by whence.  Values
        for whence are:
        
        * 0 -- start of stream (the default); offset should be zero or positive
        * 1 -- current stream position; offset may be negative
        * 2 -- end of stream; offset is usually negative
        
        Return the new absolute position.
        """
        pass
#把文件的指針移動到一個新的位置。
#格式:seek(offset[,whence]),offset表示相對於whence的位置。whence用於設置相對位置的起點,如果whence省略,offset表示相對文件開頭的位置。
#0表示從文件開頭開始計算。
#1表示從當前位置開始計算。
#2表示從文件末尾開始計算。
f = open('a.txt','r',encoding='utf-8')
print(f.read(2))    
print(f.tell())
f.seek(0)
print(f.read(3))
f.close()



    def seekable(self, *args, **kwargs): # real signature unknown
        """
        Return whether object supports random access.
        
        If False, seek(), tell() and truncate() will raise OSError.
        This method may need to do a test seek().
        """
        pass
#判斷指針是否操作
f = open('a.txt','r',encoding='utf-8')
print(f.seekable())
f.close()



    def tell(self, *args, **kwargs): # real signature unknown
        """ Return current stream position. """
        pass
#返回指針位置
f = open('a.txt','r',encoding='utf-8')
f.read(2)
print(f.tell())	#2
f.close()



    def truncate(self, *args, **kwargs): # real signature unknown
        """
        Truncate file to size bytes.
        
        File pointer is left unchanged.  Size defaults to the current IO
        position as reported by tell().  Returns the new size.
        """
        pass
#截斷數據
f = open('a.txt','r+',encoding='utf-8')
f.truncate()    #不加參數文件清空
f.close()
#加參數,從文件開頭截取
f = open('a.txt','r+',encoding='utf-8')
f.truncate(4)   #截取文件前面4個字節
f.close()



    def writable(self, *args, **kwargs): # real signature unknown
        """
        Return whether object was opened for writing.
        
        If False, write() will raise OSError.
        """
        pass
#判斷文件是否可寫
f = open('a.txt','r',encoding='utf-8')
print(f.writable()) #False,因為是用r模式打開
f.close()

 

1.5 文件的刪除

   文件的刪除需要使用os模塊和os.path模塊。os模塊提供了對系統環境、文件、目錄等操作系統級的接口函數。文件的刪除需要調用remove()函數實現。要刪除文件之前需要先判斷文件是否存在,若存在則刪除,不存在則不進行操作,如果文件被其他占用則不能刪除,報錯如下:PermissionError: [WinError 32] 另一個程序正在使用此文件,進程無法訪問。: 'a.txt'。

import os
if os.path.exists('test.txt'):
    os.remove('test.txt')

 

1.6 文件的復制

文件復制方法一:讀、寫方式復制

#邊讀邊寫
f = open('a.txt','r',encoding='utf-8')
f_new = open('a_new.txt','w',encoding='utf-8')
for line in f:
    f_new.write(line)
f.close()
f_new.close()

#或者先全部讀取,然后再寫
f = open('a.txt','r',encoding='utf-8')
f_new = open('a_new.txt','w',encoding='utf-8')
src = f.read()
f_new.write(src)
f.close()
f_new.close()

 

文件復制方法二:使用shutil模塊

import shutil

shutil.copyfile('a.txt','a1.txt')

 

1.7 文件重命名

  使用os.rename()模塊進行文件重命名。

文件重命名:

import os

li = os.listdir('.')
print(li)   #返回一個文件列表
if 'a1.txt' in li:  #判斷文件是否存在
    os.rename('a1.txt','A1.txt')

文件修改后綴:

import os

#將a.doc修改為a.html
f_list = os.listdir('.')
print(f_list)   #f_list是一個文件列表,如['a.doc', 'b.txt', 'c.txt']
for f_name in f_list:   #循環該列吧
    p = f_name.find('.')    #查找每個元素(字符串).的位置,如a.doc的位置是1。
    print(p)
    print(f_name)   #a.doc
    if f_name[p+1:] == 'doc':   #截取.后面的所有字符,如果是doc
        new_name = f_name[:p+1]+'html'  #則截取.和.前面的字符串加上html,即新字符串為a.html
        os.rename(f_name,new_name)  #將新名字重命名給舊文件上面

上面的f_name.find('.')也可以使用os.path.splitext('.')來返回一個元組實現,如下。

import os
f_list = os.listdir('.')
for f_name in f_list:
    li = os.path.splitext(f_name)  #以.截取,如('a', '.doc')
    print(li)
    if li[1] == '.doc':
        new_name = li[0]+'.html'
        os.rename(f_name,new_name)

 

1.8 文件的替換

  文件的替換原理:將原文件讀取,替換相應內容,並將新內容寫入新文件中。

 1 '''
 2 a.txt內容如下:  3 abcdefg  4 hijklmn  5 xyz  6 xyz  7 '''
 8 
 9 f = open('a.txt','r',encoding='utf-8') 10 f_new = open('aa.txt','w',encoding='utf-8') 11 
12 for line in f: 13     if 'xyz'==line: 14         line = 'XYZ'
15  f_new.write(line) 16 
17 f.close() 18 f_new.close()
 1 '''
 2 a.txt內容如下:  3 abcdefg  4 hijklmn  5 xyz  6 xyz  7 '''
 8 
 9 f = open('a.txt','r',encoding='utf-8') 10 f_new = open('aa.txt','w',encoding='utf-8') 11 
12 for line in f.readlines(): 13     f_new.write(line.replace('xyz','XYZ')) 14 
15 f.close() 16 f_new.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM