1 两种打开文件的方式
2 # file(\'a.txt\',\'\')#在python2里面
3 # open(\'a.txt\')
4
5 #r read 只能读不能写,文件不存在的时候会报错
6 #w write 文件不存在的话,帮你创建,不能读,会清空原来的文件内容
7 #a 追加模式 文件不存在的话,帮你创建,不会清空原来文件的内容,但是不能读
8
9 f = open(\'users.txt\',\'r\',encoding=\'utf-8\')
10 # res = f.read() #只能读
11 # print(res)
12
13 f.write(\'abc\') #不能写
14 运行结果:io.UnsupportedOperation: not writable #【报错】
15
16 f = open(\'users1.txt\',\'r\',encoding=\'utf-8\')
17 运行结果:FileNotFoundError: [Errno 2] No such file or directory: \'users1.txt\'
18 #【报错】
19
20 f = open(\'users2.txt\',\'w\',encoding=\'utf-8\')
21 f.write(\'abc\') #可以写
22 f.read() #不能读
23 运行结果:io.UnsupportedOperation: not readable #【报错】
24
25 f = open(\'users2.txt\',\'a\',encoding=\'utf-8\')
26 f.write(\'今天是周二\') #可以写
27 res = f.read() #不能读
28 print(res)
29 运行结果:io.UnsupportedOperation: not readable #【报错】
30
31 =============================================================================
32
33 #r+ 读写模式 文件不存在的时候会报错,可以写,但是写的有问题?
34 #w+ 写读模式 文件不存在的时候会创建,会清空原来的文件内容,可以读
35 #a+ 追加读模式,能写能读,读不到东西
36
37 #记住:只要有r 文件不存在的时候都报错
38 # 只要有w 文件内容都会被清空
39
40 #分别试
41 # 1.文件不存在的时候会不会创建
42 # 2.都能不能读写
43 # 3.能不能读到东西
44
45 f = open(\'users.txt\',\'r+\',encoding=\'utf-8\') #不存在的文件不能被创建 #【报错】
46 res = f.read() #可以读
47 print(res)
48 f.write(\'hahahah\') #可以写
49
50 f = open(\'a.txt\',\'w+\',encoding=\'utf-8\') #不存在的文件帮你创建
51 f.write(\'bacdf\') #可以写入
52 res = f.read() #可以读,但是由于w+模式会清空,导致总是什么都都不出来
53 print(res)
54
55 f = open(\'b.txt\',\'a+\',encoding=\'utf-8\') #不存在的文件帮你创建
56 f.write(\'dfadf\') #可以写入
57 res = f.read() #可以读,但是读不到东西,内容没有被清空
58 #ps:读不到东西是因为a+模式进来指针就在内容最后
59 print(res)
60
61 =============================================================================
62 #文件指针
63
64 f = open(\'b.txt\',encoding=\'utf-8\') #默认是r模式
65 # print(f.read()) #读取文件里面所有的内容,返回所有内容是字符串
66 # print(f.readline()) #读取一行的内容
67 print(f.readlines()) ##返回的是列表,一行是一个元素
68 返回结果:[\'dfadf\n\', \'hahah\n\', \'哈哈哈\n\', \'python\n\', \'ddsfs\']
69
70 print(\'read\',f.read())
71 print(\'readlines\',f.readlines())
72 返回结果:read dfadf
73 hahah
74 哈哈哈
75 python
76 ddsfs
77 readlines []
78
79 print(\'readlines\',f.readlines())
80 print(\'read\',f.read())
81 返回结果:readlines [\'dfadf\n\', \'hahah\n\', \'哈哈哈\n\', \'python\n\', \'ddsfs\']
82 read
83 注:之所以 第二行的代码都读不到内容,是因为在第一行代码读取所有内容后,文件指针就到了内容最后,所以第二行代码再去读文件的时候,就什么都都不出来
84
85
86
87
88 f = open(\'b.txt\',\'r+\',encoding=\'utf-8\')
89 f.write(\'ggg\')
90 运行结果:如下
91 文件内容:
92
93
94 ps:是因为r+模式进来指针就再最前面
95
96 ============================================================================
97
98 f = open(\'b.txt\',\'a+\',encoding=\'utf-8\')
99 l = [\'123\',\'346\',\'rdsf\'] #列表写入文件,只能用循环,因为write只能写入字符串
100 for i in l:
101 f.write(i+\'\n\')
102
103 f = open(\'b.txt\',\'a+\',encoding=\'utf-8\')
104 l = [\'123\n\',\'346\n\',\'rdsf\n\']
105 f.writelines(l) #传一个list,把list里面的每一个元素写入文件
106
107 f = open(\'b.txt\',\'a+\',encoding=\'utf-8\')
108 f.seek(0)
109 f.truncate()#从文件指针的位置删除内容
110 运行结果:文件里面的内容都被清空
111
112 f = open(\'b.txt\',\'a+\',encoding=\'utf-8\')
113 f.seek(0)
114 print(f.tell())#当前文件指针的位置
115 f.readline()
116 print(f.tell())
117
118 f = open(\'a.txt\') #第一种打开文件方法,需要去关闭
119 f.close()
120
121 with open(\'a.txt\') as f:
122 f.read()
123 #这两种打开文件的方法都可以,第二种with open方法不需要手动去关闭文件
124
125 with open(\'a.txt\') as f,with open(\'b.txt\') as f1:可以同时打开多个文件
126
127 =============================================================================
128
129 with open(\'users.txt\',encoding=\'utf-8\') as f:
130 for line in f:
131 print(\'每次循环的内容是\',line)
132
133 #文件循环 展示的是 一行一行的内容
1 文件内容:
2 bacdfs
3 dfsdf
4 gfdhbhes
5 sdfgrth
6 cdftrys
7 jytufs
8 hdhyus
9
10 :把文件中的小写的s修改成大写的S
11
12 f = open(\'a.txt\',\'a+\')
13 f.seek(0)
14 res = f.read().replace(\'s\',\'S\')#替换完是新的需要重新写入
15 f.seek(0)
16 f.truncate()
17 f.write(res)
18
19 #1.简单直接粗暴的 替换完删除旧的写入新的
20 #1 先读取到所有的内容,修改
21 #2 然后清空原有文件所有内容,在写入新的
22
23 =========================================================================
24 文件内容:xiaohei,80,91,100,90
25 xiaohei,80,93,100,91
26 xiaohei,80,90,100,90
27 xiaohei,80,93,100,91
28 xiaohei,80,90,100,90
29 xiaohei,80,94,100,91
30 xiaohei,80,90,100,90
31 xiaohei,80,90,100,90
32 xiaohei,80,94,100,91
33
34 :求平均值,并且把平均值展示到最后一列
35 f = open(\'account.txt\',\'a+\')
36 f.seek(0)
37 l = []
38 for line in f:
39 line = line.strip()
40 line_list = line.split(\',\')
41 sum = int(line_list[1])+int(line_list[2])+int(line_list[3])
42 avg = sum//3 #两个//就能使平均数去掉小数点展示整数
43 new_line = \'%s,%s\n\'%(line,avg)
44 # print(\'new_line\',new_line)
45 l.append(new_line)
46 f.seek(0)
47 f.truncate()
48 f.writelines(l)
49
50 #2.高效的方式
51 # 打开2个文件
52 # 只负责读
53
54 import os
55 with open(\'account.txt\') as f,open(\'account2.txt\',\'w\') as fw:
56 for line in f:
57 line = line.strip()
58 line_list = line.split(\',\')
59 sum = int(line_list[1])+int(line_list[2])+int(line_list[3])
60 avg = sum//3 #两个//就能使平均数去掉小数点展示整数
61 new_line = \'%s,%s\n\'%(line,avg)
62 fw.write(new_line)
63 fw.flush()#立即写入文件
64 os.remove(\'account.txt\')
65 os.rename(\'account2\',\'account.txt\')