python day 8 文件的基本操作

文件操作的基本参数:

1、文件路径;

2、编码方式;

3、操作方式:只读,只写,追加,读写,写读...

读:

r,只读。

  1、文件已什么编码方式储存的,就以什么编码方式打开。

    #编码不一致时:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb7 in position 0:                        invalid start byte

  2、文件路径。

    绝对路径:从根目录开始,一级一级查找直到找到文件。

    相对路径:在同一文件夹下,直接写文件名即可。

     #'c:\log.txt'   #/U//UU

      第一种:r'c:\log.txt'

      第二种:c:\\log.txt

         五种方式:

                1、 全部读出来f.read()

                 

1 # f = open('log',encoding='utf-8')
2                 # content = f.read()
3                 # print(content,type(content))
4                 # f.close()
View Code

      2、一行一行的读 f.readline

1 # f = open('log',encoding='utf-8')
2                 # print(f.readline())
3                 # print(f.readline())
4                 # print(f.readline())
5                 # print(f.readline())
6                 # print(f.readline())
7                 # f.close()
View Code

      3、将原文件的每一行作为一个列表的元素。f.readlines

1 # f = open('log',encoding='utf-8')
2                 # print(f.readlines())
3                 # f.close()
View Code

      4、读取一部分  read(n),在r模式下按照字符去读,rb模式下按照字节去读。

1 # 在r模式下,read(n)按照字符去读取。
2                 # 在rb模式下,read(n)按照字节去读取。
3                 # f = open('log',encoding='utf-8')
4                 # print(f.read(3))
5                 # f.close()
6                 # f = open('log',mode='rb')
7                 # content = f.read(4)
8                 # print(content)
9                 # f.close()
View Code

      5、循环读取  for i in f:

1    # f = open('log',encoding='utf-8')
2                 # for i in f:
3                 #     print(i.strip())
4                 # f.close()
View Code

rb,只读,以bytes类型读取。

    非文字类的文件时,用rb

 f = open('D:\护士老师主妇空姐联系方式.txt',mode='rb')

content = f.read()

print(content)

f.close()

r+ 先读后追加 一定要先读后写

 1  r+ 先读,后追加 一定要先读后写
 2         # f = open('log',encoding='utf-8',mode='r+')
 3         # content = f.read()
 4         # print(content)
 5         # f.write('aaa')
 6         # f.close()
 7 
 8         #错误的
 9         # f = open('log',encoding='utf-8',mode='r+')
10         # f.write('BBB')
11         # content = f.read()
12         # print(content)
13         # f.close()
View Code

r+b

写   

W

      没有文件,创建一个文件写入内容

1 # f = open('log1',encoding='utf-8',mode='w')
2                 # f.write('儿科王金发;剪短发了肯定撒就废了;就')
3                 # f.close()
View Code

                 有文件,将原文件内容清空,在写入内容。

1 # f = open('log1',encoding='utf-8',mode='w')
2                 # f.write('666')
3                 # f.close()
View Code

wb

1 # f = open('log',mode='wb')
2                 # f.write('老男孩教育'.encode('utf-8'))
3                 # f.close()
View Code

w+

w+b

 

追加: a

           ab

    a+

    a+b

操作方法:
read
read(n)
readline()
readlines()
tell
seek
truncate
writable()
readable()
  1 # f = open('D:\护士老师主妇空姐联系方式.txt',encoding='utf-8',mode='r')
  2 # content = f.read()
  3 # print(content)
  4 # f.close()
  5 '''
  6 f:变量,f_obj,file,f_handler,...文件句柄。
  7 open windows的系统功能,
  8 windows默认编码方式:gbk,linux默认编码方式utf-8。
  9 f.close()
 10 
 11 流程:打开一个文件,产生一个文件句柄,
 12       对文件句柄进行操作,关闭文件。
 13 '''
 14 
 15 # f = open('D:\护士老师主妇空姐联系方式.txt',encoding='gb2312')
 16 # 1:全部读出来f.read()
 17 # f = open('log',encoding='utf-8')
 18 # content = f.read()
 19 # print(content,type(content))
 20 # f.close()
 21 
 22 #2:一行一行的读
 23 # f = open('log',encoding='utf-8')
 24 # print(f.readline())
 25 # print(f.readline())
 26 # print(f.readline())
 27 # print(f.readline())
 28 # print(f.readline())
 29 # f.close()
 30 
 31 #3:将原文件的每一行作为一哥列表的元素。
 32 # f = open('log',encoding='utf-8')
 33 # print(f.readlines())
 34 # f.close()
 35 
 36 #4:读取一部分read(n)。
 37 # 在r模式下,read(n)按照字符去读取。
 38 # 在rb模式下,read(n)按照字节去读取。
 39 # f = open('log',encoding='utf-8')
 40 # print(f.read(3))
 41 # f.close()
 42 # f = open('log',mode='rb')
 43 # content = f.read(4)
 44 # print(content)
 45 # f.close()
 46 
 47 #5:循环读取。
 48 
 49 # f = open('log',encoding='utf-8')
 50 # for i in f:
 51 #     print(i.strip())
 52 # f.close()
 53 
 54 
 55 
 56 #非文字类的文件时,用rb
 57 # f = open('D:\护士老师主妇空姐联系方式.txt',mode='rb')
 58 # content = f.read()
 59 # print(content)
 60 # f.close()
 61 
 62 # w
 63 #没有文件,创建一个文件写入内容
 64 # f = open('log1',encoding='utf-8',mode='w')
 65 # f.write('儿科王金发;剪短发了肯定撒就废了;就')
 66 # f.close()
 67 #有文件,将原文件内容清空,在写入内容。
 68 # f = open('log1',encoding='utf-8',mode='w')
 69 # f.write('666')
 70 # f.close()
 71 
 72 # wb
 73 # f = open('log',mode='wb')
 74 # f.write('老男孩教育'.encode('utf-8'))
 75 # f.close()
 76 
 77 # a
 78 #没有文件,创建一个文件追加内容
 79 # f = open('log2',encoding='utf-8',mode='a')
 80 # f.write('666')
 81 # f.close()
 82 # 有文件,直接追加内容。
 83 # f = open('log2',encoding='utf-8',mode='a')
 84 # f.write('666')
 85 # f.close()
 86 
 87 # r+ 先读,后追加 一定要先读后写
 88 # f = open('log',encoding='utf-8',mode='r+')
 89 # content = f.read()
 90 # print(content)
 91 # f.write('aaa')
 92 # f.close()
 93 
 94 #错误的
 95 # f = open('log',encoding='utf-8',mode='r+')
 96 # f.write('BBB')
 97 # content = f.read()
 98 # print(content)
 99 # f.close()
100 
101 # w+ 先写后读。
102 # f = open('log',encoding='utf-8',mode='w+')
103 # f.write('中国')
104 # #print(f.tell())  # 按字节去读光标位置
105 # f.seek(3)  # 按照字节调整光标位置
106 # print(f.read())
107 # f.close()
108 #w+b
109 
110 
111 #a+ 追加读
112 # f = open('log',encoding='utf-8',mode='a+')
113 # f.write('BBB')
114 # content = f.read()
115 # print(content)
116 # f.close()
117 #a+b
118 
119 #其他方法
120 # f = open('log',encoding='utf-8')
121 # print(f.read())
122 # print(f.writable())
123 # f.close()
124 
125 # f = open('log',encoding='utf-8',mode='a')
126 # f.truncate(7) # 按字节对原文件截取
127 # f.close()
128 #功能一:自动关闭文件句柄。
129 #功能二:一次性操作多个文件句柄。
130 # with open('log',encoding='utf-8') as f:
131 #     print(f.read())
132 # with open('log1',encoding='utf-8') as f1:
133 #     print(f1.read())
134 
135 # with open('log',encoding='utf-8') as f1,\
136 #     open('log1',encoding='utf-8') as f2:
137 #     print(f1.read())
138 #     print(f2.read())
139 
140 # 1,将原文件读取到内存。
141 # 2,在内存中进行修改,形成新的内容。
142 # 3,将新的字符串写入新文件。
143 # 4,将原文件删除。
144 # 5,将新文件重命名成原文件。
145 import os
146 with open('log',encoding='utf-8') as f1,\
147     open('log.bak',encoding='utf-8',mode='w') as f2:
148     content = f1.read()
149     new_content = content.replace('alex','SB')
150     f2.write(new_content)
151 os.remove('log')
152 os.rename('log.bak','log')
153 
154 import os
155 with open('log',encoding='utf-8') as f1,\
156     open('log.bak',encoding='utf-8',mode='w') as f2:
157     for i in f1:
158         new_i = i.replace('SB','alex')
159         f2.write(new_i)
160 os.remove('log')
161 os.rename('log.bak','log')
View Code
import os
whit open('log',encode='utf-8',mode='r') as f1,\
        open('log.bak',encode='utf-8',mode='w') as f1:
for i in f1:
      new_i = i.replace('alex','SB')
      f2.write(new_i)
os.remove('log')
os.rename('log.bak','log')

 

            

posted @ 2018-03-27 21:06  大白1#  阅读(48)  评论(0)    收藏  举报