python【第八篇】操作文件

文件处理流程

1.打开文件,得到文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件

文件基本操作

2.1 文件操作基本流程初探

f = open('chenli.txt') #打开文件
first_line = f.readline()
print('first line:',first_line) #读一行
print('我是分隔线'.center(50,'-'))
data = f.read()# 读取剩下的所有内容,文件大时不要用
print(data) #打印读取内容
 
f.close() #关闭文件

2.2 文件编码

#不指定打开编码,默认使用操作系统的编码,windows为gbk,linux为utf-8,与解释器编码无关
#文件用什么编码,就用什么解码

2.3 文件打开模式

文件句柄 = open('文件路径', '模式')

打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。

打开文件的模式有:

  • r ,只读模式【默认】
  • w,只写模式【不可读;不存在则创建;存在则清空内容;】
  • x, 只写模式【不可读;不存在则创建,存在则报错】
  • a, 追加模式【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+, 读写【可读,可写】
  • w+,写读【可读,可写】
  • x+ ,写读【可读,可写】
  • a+, 写读【可读,可写】

 "b"表示以字节的方式操作

  • rb  或 r+b
  • wb 或 w+b
  • xb 或 w+b
  • ab 或 a+b

 注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型

方法详解

class file(object)
    def close(self): # real signature unknown; restored from __doc__
        关闭文件
        """
        close() -> None or (perhaps) an integer.  Close the file.
         
        Sets data attribute .closed to True.  A closed file cannot be used for
        further I/O operations.  close() may be called more than once without
        error.  Some kinds of file objects (for example, opened by popen())
        may return an exit status upon closing.
        """
 
    def fileno(self): # real signature unknown; restored from __doc__
        文件描述符  
         """
        fileno() -> integer "file descriptor".
         
        This is needed for lower-level file interfaces, such os.read().
        """
        return 0    
 
    def flush(self): # real signature unknown; restored from __doc__
        刷新文件内部缓冲区
        """ flush() -> None.  Flush the internal I/O buffer. """
        pass
 
 
    def isatty(self): # real signature unknown; restored from __doc__
        判断文件是否是同意tty设备
        """ isatty() -> true or false.  True if the file is connected to a tty device. """
        return False
 
 
    def next(self): # real signature unknown; restored from __doc__
        获取下一行数据,不存在,则报错
        """ x.next() -> the next value, or raise StopIteration """
        pass
 
    def read(self, size=None): # real signature unknown; restored from __doc__
        读取指定字节数据
        """
        read([size]) -> read at most size bytes, returned as a string.
         
        If the size argument is negative or omitted, read until EOF is reached.
        Notice that when in non-blocking mode, less data than what was requested
        may be returned, even if no size parameter was given.
        """
        pass
 
    def readinto(self): # real signature unknown; restored from __doc__
        读取到缓冲区,不要用,将被遗弃
        """ readinto() -> Undocumented.  Don't use this; it may go away. """
        pass
 
    def readline(self, size=None): # real signature unknown; restored from __doc__
        仅读取一行数据
        """
        readline([size]) -> next line from the file, as a string.
         
        Retain newline.  A non-negative size argument limits the maximum
        number of bytes to return (an incomplete line may be returned then).
        Return an empty string at EOF.
        """
        pass
 
    def readlines(self, size=None): # real signature unknown; restored from __doc__
        读取所有数据,并根据换行保存值列表
        """
        readlines([size]) -> list of strings, each a line from the file.
         
        Call readline() repeatedly and return a list of the lines so read.
        The optional size argument, if given, is an approximate bound on the
        total number of bytes in the lines returned.
        """
        return []
 
    def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
        指定文件中指针位置
        """
        seek(offset[, whence]) -> None.  Move to new file position.
         
        Argument offset is a byte count.  Optional argument whence defaults to
(offset from start of file, offset should be >= 0); other values are 1
        (move relative to current position, positive or negative), and 2 (move
        relative to end of file, usually negative, although many platforms allow
        seeking beyond the end of a file).  If the file is opened in text mode,
        only offsets returned by tell() are legal.  Use of other offsets causes
        undefined behavior.
        Note that not all file objects are seekable.
        """
        pass
 
    def tell(self): # real signature unknown; restored from __doc__
        获取当前指针位置
        """ tell() -> current file position, an integer (may be a long integer). """
        pass
 
    def truncate(self, size=None): # real signature unknown; restored from __doc__
        截断数据,仅保留指定之前数据
        """
        truncate([size]) -> None.  Truncate the file to at most size bytes.
         
        Size defaults to the current file position, as returned by tell().
        """
        pass
 
    def write(self, p_str): # real signature unknown; restored from __doc__
        写内容
        """
        write(str) -> None.  Write string str to file.
         
        Note that due to buffering, flush() or close() may be needed before
        the file on disk reflects the data written.
        """
        pass
 
    def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
        将一个字符串列表写入文件
        """
        writelines(sequence_of_strings) -> None.  Write the strings to the file.
         
        Note that newlines are not added.  The sequence can be any iterable object
        producing strings. This is equivalent to calling write() for each string.
        """
        pass
 
    def xreadlines(self): # real signature unknown; restored from __doc__
        可用于逐行读取文件,非全部
        """
        xreadlines() -> returns self.
         
        For backward compatibility. File objects now include the performance
        optimizations previously implemented in the xreadlines module.
        """
        pass

2.x
2,x
class TextIOWrapper(_TextIOBase):
    """
    Character and line based layer over a BufferedIOBase object, buffer.
    
    encoding gives the name of the encoding that the stream will be
    decoded or encoded with. It defaults to locale.getpreferredencoding(False).
    
    errors determines the strictness of encoding and decoding (see
    help(codecs.Codec) or the documentation for codecs.register) and
    defaults to "strict".
    
    newline controls how line endings are handled. It can be None, '',
    '\n', '\r', and '\r\n'.  It works as follows:
    
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
      these are translated into '\n' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
    
    * On output, if newline is None, any '\n' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '\n', no translation takes place. If newline is any
      of the other legal values, any '\n' characters written are translated
      to the given string.
    
    If line_buffering is True, a call to flush is implied when a call to
    write contains a newline character.
    """
    def close(self, *args, **kwargs): # real signature unknown
        关闭文件
        pass

    def fileno(self, *args, **kwargs): # real signature unknown
        文件描述符  
        pass

    def flush(self, *args, **kwargs): # real signature unknown
        刷新文件内部缓冲区
        pass

    def isatty(self, *args, **kwargs): # real signature unknown
        判断文件是否是同意tty设备
        pass

    def read(self, *args, **kwargs): # real signature unknown
        读取指定字节数据
        pass

    def readable(self, *args, **kwargs): # real signature unknown
        是否可读
        pass

    def readline(self, *args, **kwargs): # real signature unknown
        仅读取一行数据
        pass

    def seek(self, *args, **kwargs): # real signature unknown
        指定文件中指针位置
        pass

    def seekable(self, *args, **kwargs): # real signature unknown
        指针是否可操作
        pass

    def tell(self, *args, **kwargs): # real signature unknown
        获取指针位置
        pass

    def truncate(self, *args, **kwargs): # real signature unknown
        截断数据,仅保留指定之前数据
        pass

    def writable(self, *args, **kwargs): # real signature unknown
        是否可写
        pass

    def write(self, *args, **kwargs): # real signature unknown
        写内容
        pass

    def __getstate__(self, *args, **kwargs): # real signature unknown
        pass

    def __init__(self, *args, **kwargs): # real signature unknown
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __next__(self, *args, **kwargs): # real signature unknown
        """ Implement next(self). """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    buffer = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    closed = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    encoding = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    errors = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    name = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    newlines = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

3.x
3.x

2.4 文件内置函数flush

flush原理:

文件操作是通过软件将文件从硬盘读到内存
写入文件的操作也都是存入内存缓冲区buffer(内存速度快于硬盘,如果写入文件的数据都从内存刷到硬盘,内存与硬盘的速度延迟会被无限放大,效率变低,所以要刷到硬盘的数据我们统一往内存的一小块空间即buffer中放,一段时间后操作系统会将buffer中数据一次性刷到硬盘)
flush即,强制将写入的数据刷到硬盘
滚动条:

import sys,time

for i in  range(10):
    sys.stdout.write('#')
    sys.stdout.flush()
    time.sleep(0.2)

或者

import time
for i in range(10):
    print('#',end='',flush=True)
    time.sleep(0.2)
else:
    print()

2.5 文件内光标移动

一: read(3):

  1. 文件打开方式为文本模式时,代表读取3个字符

  2. 文件打开方式为b模式时,代表读取3个字节

二: 其余的文件内光标移动都是以字节为单位如seek,tell,truncate

注意:

  1. seek有三种移动方式0,1,2,其中1和2必须在b模式下进行,但无论哪种模式,都是以bytes为单位移动的

  2. truncate是截断文件,所以文件的打开方式必须可写,但是不能用w或w+等方式打开,因为那样直接清空文件了,所以truncate要在r+或a或a+等模式下测试效果

import time
with open('test.txt','rb') as f:
    f.seek(0,2)
    while True:
        line=f.readline()
        if line:
            print(line.decode('utf-8'))
        else:
            time.sleep(0.2)

2.6 open函数详解

1. open()语法

open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
open函数有很多的参数,常用的是file,mode和encoding
file文件位置,需要加引号
mode文件打开模式,见下面3
buffering的可取值有0,1,>1三个,0代表buffer关闭(只适用于二进制模式),1代表line buffer(只适用于文本模式),>1表示初始化的buffer大小;
encoding表示的是返回的数据采用何种编码,一般采用utf8或者gbk;
errors的取值一般有strict,ignore,当取strict的时候,字符编码出现问题的时候,会报错,当取ignore的时候,编码出现问题,程序会忽略而过,继续执行下面的程序。
newline可以取的值有None, \n, \r, ”, ‘\r\n',用于区分换行符,但是这个参数只对文本模式有效;
closefd的取值,是与传入的文件参数有关,默认情况下为True,传入的file参数为文件的文件名,取值为False的时候,file只能是文件描述符,什么是文件描述符,就是一个非负整数,在Unix内核的系统中,打开一个文件,便会返回一个文件描述符。

2. Python中file()与open()区别
两者都能够打开文件,对文件进行操作,也具有相似的用法和参数,但是,这两种文件打开方式有本质的区别,file为文件类,用file()来打开文件,相当于这是在构造文件类,而用open()打开文件,是用python的内建函数来操作,建议使用open

3. 参数mode的基本取值

Character Meaning
‘r' open for reading (default)
‘w' open for writing, truncating the file first
‘a' open for writing, appending to the end of the file if it exists
‘b' binary mode
‘t' text mode (default)
‘+' open a disk file for updating (reading and writing)
‘U' universal newline mode (for backwards compatibility; should not be used in new code)

r、w、a为打开文件的基本模式,对应着只读、只写、追加模式;
b、t、+、U这四个字符,与以上的文件打开模式组合使用,二进制模式,文本模式,读写模式、通用换行符,根据实际情况组合使用、

常见的mode取值组合

r或rt 默认模式,文本模式读
rb   二进制文件
    
w或wt 文本模式写,打开前文件存储被清空
wb  二进制写,文件存储同样被清空
    
a  追加模式,只能写在文件末尾
a+ 可读写模式,写只能写在文件末尾
    
w+ 可读写,与a+的区别是要清空文件内容
r+ 可读写,与a+的区别是可以写到文件任何位置

2.7 上下文管理

with open('a.txt','w') as f:
    pass

 

with open('a.txt','r') as read_f,open('b.txt','w') as write_f:
    data=read_f.read()
    write_f.write(data)

2.8 文件的修改

import os
with open('a.txt','r',encoding='utf-8') as read_f,\
        open('.a.txt.swap','w',encoding='utf-8') as write_f:
    for line in read_f:
        if line.startswith('hello'):
            line='哈哈哈\n'
        write_f.write(line)

os.remove('a.txt')
os.rename('.a.txt.swap','a.txt')

Python处理标准输入

(sys.stdin表示标准输入文件,sys.stdin代表标准输出文件没有管道符时会阻塞即等待输入)

input调用sys.stdin的read方法

print 调用sys.stdout的write方法

#!/usr/bin/python
#encoding:utf8

import sys

fd = sys.stdin
data = fd.read()

sys.stdout.write(data)
#print data
View Code
#!/usr/bin/python
#encoding:utf8

import sys
def lineCount(fd):
    n = 0
    for i in fd:
        n += 1
    return n
fd = sys.stdin
print lineCount(fd)
View Code

# wc -l /etc/hosts 3 /etc/hosts root@iZ288bqr3pkZ[22:41:07]:~ # cat /etc/hosts | python test.py 3

Python处理标准输出

In [2]: import sys

In [3]: for i in range(1,10):
    sys.stdout.write("str:%d\n" %i)
   ...:     
str:1
str:2
str:3
str:4
str:5
str:6
str:7
str:8
str:9
View Code

 模拟WC命令1

#!/usr/bin/python
#encoding:utf8

import sys

data = sys.stdin.read()

charsCount = len(data)
wordsCount = len(data.split())
linesCount = data.count('\n')

print linesCount,wordsCount,charsCount
View Code
1 # wc /etc/hosts
2   3   9 126 /etc/hosts
3 root@iZ288bqr3pkZ[16:53:34]:~
4 # cat /etc/hosts | python test.py 
5 3 9 126

  模拟WC命令2

#!/usr/bin/python 
#encoding:utf8 
 
import sys,os 
#eg: cat /etc/hosts | python .py
if len(sys.argv) == 1: #sys.argv是一个列表,第一个参数为脚本名
    data = sys.stdin.read()
#如果是普通文件,命令格式为python .py f
elif len(sys.argv) == 2:
    try:
        fn = sys.argv[1]
    except StandardError:
        print "please follow a argument as %s" % __file_
    if not os.path.exists(fn):
        print "%s is not exists" % fn
    fd = open(sys.argv[1])
    data = fd.read()
    fd.close() 
else:       
    print "syntax error , numbers of argument must be 1 or 2"
    sys.exit()
 
chars = len(data)
words = len(data.split())
lines = data.count('\n')
 
print "lines:%s,words:%s,chars:%s" % (lines,words,chars)
View Code

optparse模块实现wc命令

#!/usr/bin/python
#_*_ coding:utf8 _*_

from optparse import OptionParser

parser = OptionParser()

parser.add_option("-c","--char",
                  dest="char",
                  action="store_true",
                  default=False,
                  help="on;y count chars")


parser.add_option("-w","--words",
                  dest="words",
                  action="store_true",
                  default=False,
                  help="on;y count words")

parser.add_option("-l","--line",
                  dest="lines",
                  action="store_true",
                  default=False,
                  help="on;y count lines")

options,args = parser.parse_args()
print options,args
View Code
{'char': False, 'lines': False, 'words': False} []
# python 1.py -l a b
{'char': False, 'lines': True, 'words': False} ['a', 'b']
#!/usr/bin/python
#encoding:utf8

from optparse import OptionParser
import sys,os

#设置选项;得到选项字典和参数列表
def option():

    parser = OptionParser()
    parser.add_option("-c","--chars",
                      dest="characters",
                      action="store_true",
                      default=False,
                      help="only count characters",)

    parser.add_option("-w","--words",
                      dest="words",
                      action="store_true",
                      default=False,
                      help="only count words",)

    parser.add_option("-l","--lines",
                      dest="lines",
                      action="store_true",
                      default=False,
                      help="only count lines",)
    options, args = parser.parse_args()
    return options,args

#通过文件内容得到目的数据
def getCount(data):
    chars = len(data)
    words = len(data.split())
    lines = data.count('\n')
    return lines,words,chars
    
#通过选项和目的数据打印结果
def printResult(options,lines,words,chars,fn):
    if options.lines:
        print lines,
    if options.words:
        print words,
    if options.characters:
        print chars,
    print fn

    
def main():
    #得到选项和参数
    options,args = option() 
    #参数一个都不出现则默认加上全部参数
    if not (options.characters or options.words or options.lines):
        options.characters,options.words,options.lines = True,True,True
    
    #通过参数得到文件名,进而得到文件内容
    if args:
        for fn in args:
            with open(fn) as fd:
                data = fd.read()
            #得到目的数据
            lines,words,chars = getCount(data)
            #通过选项和目的数据打印结果
            printResult(options,lines,words,chars,fn)
    else:
        fn = ''
        data = sys.stdin.read()
        lines,words,chars = getCount(data)
        printResult(options,lines,words,chars,fn)


if __name__ == '__main__':
    main()
View Code
# python test.py 1.py  test.py 
13 21 259 1.py
73 125 1947 test.py

支持:sys.sterr显示自定义报错(而不是一大堆系统提示的错误);2> 和1>重定向

#!/usr/bin/python
#encoding:utf8

from optparse import OptionParser
import sys,os

#设置选项;得到选项字典和参数列表
def option():

    parser = OptionParser()
    parser.add_option("-c","--chars",
                      dest="characters",
                      action="store_true",
                      default=False,
                      help="only count characters",)

    parser.add_option("-w","--words",
                      dest="words",
                      action="store_true",
                      default=False,
                      help="only count words",)

    parser.add_option("-l","--lines",
                      dest="lines",
                      action="store_true",
                      default=False,
                      help="only count lines",)
    options, args = parser.parse_args()
    return options,args

#通过文件内容得到目的数据
def getCount(data):
    chars = len(data)
    words = len(data.split())
    lines = data.count('\n')
    return lines,words,chars
    
#通过选项和目的数据打印结果
def printResult(options,lines,words,chars,fn):
    if options.lines:
        print lines,
    if options.words:
        print words,
    if options.characters:
        print chars,
    print fn


def main():
    #得到选项和参数
    options,args = option() 
    #参数一个都不出现则默认加上全部参数
    if not (options.characters or options.words or options.lines):
        options.characters,options.words,options.lines = True,True,True
    
    #通过参数得到文件名,进而得到文件内容
    if args:
        total_lines,total_words,total_chars = 0,0,0
        for fn in args:
            if  os.path.isfile(fn):
                with open(fn) as fd:
                    data = fd.read()
                #得到目的数据
                lines,words,chars = getCount(data)
                #通过选项和目的数据打印结果
                printResult(options,lines,words,chars,fn)
                total_lines += lines
                total_words += words
                total_chars += chars
            
            elif os.path.isdir(fn):
                print >> sys.sterr,"%s:is a directory" % fn
            else:
                sys.stderr.write("%s:No such file or directory\n" % fn)
            
        #得到所有文件的总数
        if len(args) > 1:
            printResult(options,total_lines,total_words,total_chars,'total')
    else:
        fn = ''
        data = sys.stdin.read()
        #得到目的数据
        lines,words,chars = getCount(data)
        #通过选项和目的数据打印结果
        printResult(options,lines,words,chars,fn)

    
    


if __name__ == '__main__':
    main()
View Code
# python test.py test.py 
92 164 2741 test.py

# python test.py test.py test.py 
92 164 2741 test.py
92 164 2741 test.py
184 328 5482 total

# python test.py test.p
test.p:No such file or directory

# python test.py mulu
mulu:is a directory

  

  

 

posted @ 2017-01-18 21:54  沐风先生  阅读(352)  评论(0编辑  收藏  举报