Python文件处理

Posted 将暮未暮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python文件处理相关的知识,希望对你有一定的参考价值。

1. 文件的操作

1.1 open操作

格式:

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, ‘‘, ‘
‘, ‘
‘, and ‘
‘.  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 ‘
‘, ‘
‘, or ‘
‘, and
 86       these are translated into ‘
‘ 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 ‘
‘ characters written are
 93       translated to the system default line separator, os.linesep. If
 94       newline is ‘‘ or ‘
‘, no translation takes place. If newline is any
 95       of the other legal values, any ‘
‘ 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 或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
‘, ‘    dsfsdfs
‘, ‘hijklmn‘]
f.close()

 

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.2 文件的写入

 

 

2. 目录的操作

 

 

3. 文件和流

 

 


























以上是关于Python文件处理的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

你如何在 python 中处理 graphql 查询和片段?

python+spark程序代码片段

我在哪里更改此 Python 代码片段以将临时文件保存在 tmp 文件夹中?

15种Python片段去优化你的数据科学管道

使用 Python 代码片段编写 LaTeX 文档