Python 第三天 文件操作

文件操作

操作文件时,一般需要经历如下步骤:

  • 打开文件
  • 操作文件

一、打开

文件句柄 = file(‘文件路径‘‘模式‘)

注:python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open。 open会自己在Python中找。

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

打开文件的模式有:

  • r,只读模式(默认)。
  • w,只写模式。【不可读,也就是重写这个文件,首先就是先清空原来的内容,然后重写;不存在则创建;存在则删除内容;】
  • a,追加模式。【可读,可写;不存在则创建;存在则只追加内容;】 默认指针应该在文件的最后端。

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

  • r+,可读写文件。【可读;可写;可追加】
  • w+,写读 等同于w
  • a+,同a
1 例如:
2     obj = open(‘log‘,‘r+‘)
3     obj.write(‘0000‘)
4     obj.truncate()   默认截断数据,根据当前指针截断,如果truncate(5),就只保留前5个,
5     obj.close()
6  在r+的情况下,如果要write,就是文件指针在最开始,然后0000去一个一个替换。

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用),只能与r一起用。

  • rU
  • r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

  • rb
  • wb
  • ab

二、操作操作

  1 class file(object):
  2
  3     def close(self): # real signature unknown; restored from __doc__
  4         关闭文件
  5         """
  6         close() -> None or (perhaps) an integer.  Close the file.
  7
  8         Sets data attribute .closed to True.  A closed file cannot be used for
  9         further I/O operations.  close() may be called more than once without
 10         error.  Some kinds of file objects (for example, opened by popen())
 11         may return an exit status upon closing.
 12         """
 13
 14     def fileno(self): # real signature unknown; restored from __doc__
 15         文件描述符
 16          """
 17         fileno() -> integer "file descriptor".
 18
 19         This is needed for lower-level file interfaces, such os.read().
 20         """
 21         return 0
 22
 23     def flush(self): # real signature unknown; restored from __doc__
 24         刷新文件内部缓冲区
 25         """ flush() -> None.  Flush the internal I/O buffer. """
 26         pass
 27
 28
 29     def isatty(self): # real signature unknown; restored from __doc__
 30         判断文件是否是同意tty设备
 31         """ isatty() -> true or false.  True if the file is connected to a tty device. """
 32         return False
 33
 34
 35     def next(self): # real signature unknown; restored from __doc__
 36         获取下一行数据,不存在,则报错
 37         """ x.next() -> the next value, or raise StopIteration """
 38         pass
 39
 40     def read(self, size=None): # real signature unknown; restored from __doc__
 41         读取指定字节数据   默认读取所有字节
 42         """
 43         read([size]) -> read at most size bytes, returned as a string.
 44
 45         If the size argument is negative or omitted, read until EOF is reached.
 46         Notice that when in non-blocking mode, less data than what was requested
 47         may be returned, even if no size parameter was given.
 48         """
 49         pass
 50
 51     def readinto(self): # real signature unknown; restored from __doc__
 52         读取到缓冲区,不要用,将被遗弃
 53         """ readinto() -> Undocumented.  Don‘t use this; it may go away. """
 54         pass
 55
 56     def readline(self, size=None): # real signature unknown; restored from __doc__
 57         仅读取一行数据
 58         """
 59         readline([size]) -> next line from the file, as a string.
 60
 61         Retain newline.  A non-negative size argument limits the maximum
 62         number of bytes to return (an incomplete line may be returned then).
 63         Return an empty string at EOF.
 64         """
 65         pass
 66
 67     def readlines(self, size=None): # real signature unknown; restored from __doc__
 68         读取所有数据,并根据换行保存值列表
 69         """
 70         readlines([size]) -> list of strings, each a line from the file.
 71
 72         Call readline() repeatedly and return a list of the lines so read.
 73         The optional size argument, if given, is an approximate bound on the
 74         total number of bytes in the lines returned.
 75         """
 76         return []
 77
 78     def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
 79         指定文件中指针位置  seek(5) 指定文件指针从第5个字符开始读取文件。
 80         """
 81         seek(offset[, whence]) -> None.  Move to new file position.
 82
 83         Argument offset is a byte count.  Optional argument whence defaults to
 84         0 (offset from start of file, offset should be >= 0); other values are 1
 85         (move relative to current position, positive or negative), and 2 (move
 86         relative to end of file, usually negative, although many platforms allow
 87         seeking beyond the end of a file).  If the file is opened in text mode,
 88         only offsets returned by tell() are legal.  Use of other offsets causes
 89         undefined behavior.
 90         Note that not all file objects are seekable.
 91         """
 92         pass
 93
 94     def tell(self): # real signature unknown; restored from __doc__
 95         获取当前指针位置,也就是一个文件开始从哪开始读。             例如: obj = open(‘logs‘,‘r‘)                   obj.seek(5)  ###把文件指针放到第5个字节,也就是从第5个字节开始读                   print obj.tell()#获取当前的指针                   print obj.read()                   print obj.tell()                    obj.close()
 96         """ tell() -> current file position, an integer (may be a long integer). """
 97         pass
 98
 99     def truncate(self, size=None): # real signature unknown; restored from __doc__
100         截断数据,仅保留指定之前数据.默认情况下指针在哪,后面的就全部不要了。
101         """
102         truncate([size]) -> None.  Truncate the file to at most size bytes.
103
104         Size defaults to the current file position, as returned by tell().
105         """
106         pass
107
108     def write(self, p_str): # real signature unknown; restored from __doc__
109         写内容
110         """
111         write(str) -> None.  Write string str to file.
112
113         Note that due to buffering, flush() or close() may be needed before
114         the file on disk reflects the data written.
115         """
116         pass
117
118     def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
119         将一个字符串列表写入文件
120         """
121         writelines(sequence_of_strings) -> None.  Write the strings to the file.
122
123         Note that newlines are not added.  The sequence can be any iterable object
124         producing strings. This is equivalent to calling write() for each string.
125         """
126         pass
127
128     def xreadlines(self): # real signature unknown; restored from __doc__
129         可用于逐行读取文件,非全部
130         """
131         xreadlines() -> returns self.
132
133         For backward compatibility. File objects now include the performance
134         optimizations previously implemented in the xreadlines module.
135         """
136         pass

三、with

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

1 with open(‘log‘,‘r‘) as f:  ##打开文件的同时,并建立文件句柄
2      f.write(xxxxxx)
3     ...

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

1 with open(‘log1‘,‘r’) as obj1, open(‘log2‘,‘w‘) as obj2:
2     pass
old.conf ===>r   new.conf w,
with open(‘log1‘,‘r‘) as obj1,open(‘log2‘,‘w‘) as obj2;
    for line obj1:
         new_line = line.replace(‘10.0.0.1‘,‘10.0.0.2‘)
         obj2.write(new_line)

四、那么问题来了...

1、如何在线上环境优雅的修改配置文件?

                    ####原配置文件 1 global
 2         log 127.0.0.1 local2
 3         daemon
 4         maxconn 256
 5         log 127.0.0.1 local2 info
 6 defaults
 7         log global
 8         mode http
 9         timeout connect 5000ms
10         timeout client 50000ms
11         timeout server 50000ms
12         option  dontlognull
13
14 listen stats :8888
15         stats enable
16         stats uri       /admin
17         stats auth      admin:1234
18
19 frontend oldboy.org
20         bind 0.0.0.0:80
21         option httplog
22         option httpclose
23         option  forwardfor
24         log global
25         acl www hdr_reg(host) -i www.oldboy.org
26         use_backend www.oldboy.org if www
27
28 backend www.oldboy.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
      ###需求 1 1、查
 2     输入:www.oldboy.org
 3     获取当前backend下的所有记录
 4
 5 2、新建
 6     输入:
 7         arg = {
 8             ‘bakend‘: ‘www.oldboy.org‘,
 9             ‘record‘:{
10                 ‘server‘: ‘100.1.7.9‘,
11                 ‘weight‘: 20,
12                 ‘maxconn‘: 30
13             }
14         }
15
16 3、删除
17     输入:
18         arg = {
19             ‘bakend‘: ‘www.oldboy.org‘,
20             ‘record‘:{
21                 ‘server‘: ‘100.1.7.9‘,
22                 ‘weight‘: 20,
23                 ‘maxconn‘: 30
24             }
25         }
       ####dome  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 import json
  4 import os
  5
  6
  7 def fetch(backend):
  8     backend_title = ‘backend %s‘ % backend
  9     record_list = []
 10     with open(‘ha‘) as obj:
 11         flag = False
 12         for line in obj:
 13             line = line.strip()
 14             if line == backend_title:
 15                 flag = True
 16                 continue
 17             if flag and line.startswith(‘backend‘):
 18                 flag = False
 19                 break
 20
 21             if flag and line:
 22                 record_list.append(line)
 23
 24     return record_list
 25
 26
 27 def add(dict_info):
 28     backend = dict_info.get(‘backend‘)
 29     record_list = fetch(backend)
 30     backend_title = "backend %s" % backend
 31     current_record = "server %s %s weight %d maxconn %d" % (dict_info[‘record‘][‘server‘], dict_info[‘record‘][‘server‘], dict_info[‘record‘][‘weight‘], dict_info[‘record‘][‘maxconn‘])
 32     if not record_list:
 33         record_list.append(backend_title)
 34         record_list.append(current_record)
 35         with open(‘ha‘) as read_file, open(‘ha.new‘, ‘w‘) as write_file:
 36             flag = False
 37             for line in read_file:
 38                 write_file.write(line)
 39             for i in record_list:
 40                 if i.startswith(‘backend‘):
 41                     write_file.write(i+‘\n‘)
 42                 else:
 43                     write_file.write("%s%s\n" % (8*" ", i))
 44     else:
 45         record_list.insert(0, backend_title)
 46         if current_record not in record_list:
 47             record_list.append(current_record)
 48
 49         with open(‘ha‘) as read_file, open(‘ha.new‘, ‘w‘) as write_file:
 50             flag = False
 51             has_write = False
 52             for line in read_file:
 53                 line_strip = line.strip()
 54                 if line_strip == backend_title:
 55                     flag = True
 56                     continue
 57                 if flag and line_strip.startswith(‘backend‘):
 58                     flag = False
 59                 if not flag:
 60                     write_file.write(line)
 61                 else:
 62                     if not has_write:
 63                         for i in record_list:
 64                             if i.startswith(‘backend‘):
 65                                 write_file.write(i+‘\n‘)
 66                             else:
 67                                 write_file.write("%s%s\n" % (8*" ", i))
 68                     has_write = True
 69     os.rename(‘ha‘,‘ha.bak‘)
 70     os.rename(‘ha.new‘,‘ha‘)
 71
 72
 73 def remove(dict_info):
 74     backend = dict_info.get(‘backend‘)
 75     record_list = fetch(backend)
 76     backend_title = "backend %s" % backend
 77     current_record = "server %s %s weight %d maxconn %d" % (dict_info[‘record‘][‘server‘], dict_info[‘record‘][‘server‘], dict_info[‘record‘][‘weight‘], dict_info[‘record‘][‘maxconn‘])
 78     if not record_list:
 79         return
 80     else:
 81         if current_record not in record_list:
 82             return
 83         else:
 84             del record_list[record_list.index(current_record)]
 85             if len(record_list) > 0:
 86                 record_list.insert(0, backend_title)
 87         with open(‘ha‘) as read_file, open(‘ha.new‘, ‘w‘) as write_file:
 88             flag = False
 89             has_write = False
 90             for line in read_file:
 91                 line_strip = line.strip()
 92                 if line_strip == backend_title:
 93                     flag = True
 94                     continue
 95                 if flag and line_strip.startswith(‘backend‘):
 96                     flag = False
 97                 if not flag:
 98                     write_file.write(line)
 99                 else:
100                     if not has_write:
101                         for i in record_list:
102                             if i.startswith(‘backend‘):
103                                 write_file.write(i+‘\n‘)
104                             else:
105                                 write_file.write("%s%s\n" % (8*" ", i))
106                     has_write = True
107     os.rename(‘ha‘,‘ha.bak‘)
108     os.rename(‘ha.new‘,‘ha‘)
109
110 if __name__ == ‘__main__‘:
111     """
112     print ‘1、获取;2、添加;3、删除‘
113     num = raw_input(‘请输入序号:‘)
114     data = raw_input(‘请输入内容:‘)
115     if num == ‘1‘:
116         fetch(data)
117     else:
118         dict_data = json.loads(data)
119         if num == ‘2‘:
120             add(dict_data)
121         elif num == ‘3‘:
122             remove(dict_data)
123         else:
124             pass
125     """
126     #data = "www.oldboy.org"
127     #fetch(data)
128     #data = ‘{"backend": "tettst.oldboy.org","record":{"server": "100.1.7.90","weight": 20,"maxconn": 30}}‘
129     #dict_data = json.loads(data)
130     #add(dict_data)
131     #remove(dict_data)

2、文件处理中xreadlines的内部是如何实现的呢?

时间: 2024-08-08 14:48:58

Python 第三天 文件操作的相关文章

python入门三:文件操作

一.文件操作 1.文件对象:和c一样,要想对一个文件进行操作,需要获取该文件的对象 1 f = open("xxx") # 打开文件并获取文件对象 2 f.xxx # 对文件进行某些操作 3 f.close() # 关闭文件 2.访问模式: open函数除了接受一个文件名参数外,还可以设定文件的访问模式(open其他的参数不太能理解) 无   以只读方式打开,文件必须存在 r     以只读方式打开,文件必须存在 w    以只写方式打开, 先删除原有内容再写入新内容,文件不存在创建新

python字符串处理与文件操作

1.strip函数 strip是trim(裁剪)掉字符串两边的空格或字符.(lstrip/rstrip) 如: 空格 theString = ' abcdbcyesornoabcbacdd ' print theString.strip() abcdbcyesornoabcbacdd 字符 theString = 'abcdbcyesornoabcbacdd' print theString.strip('abcd') #去掉两端的abcd字符 yesorno 问题:如何去掉中间空格. theS

Python学习记录——Ubuntu(三)文件操作

一.mkdir用于创建目录: mkdir 目录名  #创建目录 mkdir -p 目录名1/目录名2/目录名3  #创建多层目录 二.rm用于删除文件(慎用,易引发程序崩溃): 1.参数 (1)rm 文件名   #只能删除文件,但是不能删除目录 (2)rm -i 文件名  #会出现提示 (3)rm -f 文件名  #强制删除 (4)rm -rf 目录名  #强制删除目录 三.mv用于移动或重命名文件/目录: 1.示例 (1)mv 文件/目录名 新文件/目录名  #重命名文件/目录 (2)mv  

python学习笔记——(三)文件操作

·集合操作及其相应的操作符表示集合中没有插入,只有添加,因为毕竟无序 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Vergil Zhang list_1 = [1, 4, 5, 7, 3, 6, 7, 9] list_1 = set(list_1) print(list_1, type(list_1)) list_2 = set([2, 6, 0, 66, 22, 8]) print(list_1,list_2) #交集 print

Python基础(三)文件操作和处理json

文件操作步骤:1.有一个文件,2.打开文件,3.读写修改文件,4.关闭文件 一.有一个文件:新建或导入文件 二.打开文件:如果是新建的文件默认和py文件在同一个目录:如果是打开文件,要将文件放在py同目录或者是打开文件要写绝对路径 打开文件有两种方式:1. f = open('user.txt') ; 2. with open ('user.txt') as f, open('user2.txt') as f: 两者的区别是第1种方法必须用f.close()关闭,定义一次只能打开一个文件 :第二

Python 第十三节 文件操作

A 1.首先文件读写操作有以下几种模式:   a\a+  w\w+ r\r+   a模式:追加_写入模式,写入指针默认在开头,如果文件存在将在开头追加写入,如果文件不存在将创建文件再写入. a+模式:追加_读写模式,可读可写,写入指针默认在末尾,如果文件存在将在末尾追加写入,如果文件不存在将创建文件再写入. w模式:写模式,如果文件存在,把文件覆盖再写入,如果文件不存在将创建文件再写入. w+模式:写读模式,可写可读,如果文件存在,把文件覆盖再写入,如果文件不存在将创建文件再写入. r模式:读模

Python(day5)文件操作

一.文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件打开模式 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ,只读模式[默认模式,文件必须存在,不存在则抛出异常] w,只写模式[不可读:不存在则创建:存在则清空内容] x, 只写模式[不可读:不存在则创建,存在则报错] a, 追加模式[可读:   不存在则创建:存在则只追加内容] "+" 表示可以同时

Python学习基础篇—文件操作和集合

这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 现有以下文件file.txt: 我们哭了 我们笑着 我们抬头望天空 星星还亮着几颗 我们唱着 时间的歌 才懂得相互拥抱 到底是为了什么 因为我刚好遇见你 留下足迹才美丽 风吹花落泪如雨 因为不想分离 因为刚好遇见你 留下十年的期许 如果再相遇 我想我会记得你 我们哭了 我们笑着 我们抬头望天空 星星还亮着几颗 我们唱着 时间的歌 才懂得相互

Python不归路_文件操作(一)

Python文件操作 Python文件操作的语法是: open(file,mode,buffering,encoding,erros,newline,closefd) 我们先来看下各个参数, file:很明显,输入文件的路径. mode:mode参数有很多  r 以只读方式打开文件,默认参数  r+ 以读写方式打开文件   w 以写入方式打开文件,如果文件存在,先删除原文件,再创建文件:如果文件不存在直接创建  w+ 以读写方式打开文件,如果文件存在,先删除原文件,再创建文件:如果文件不存在直接