python-fullstack-s13-day08-python基础

  1 f = open("name",mode="r",encoding="UTF-8")
  2 print(type(f))
  3 for line in f:
  4     print(line.strip())
  5 f.close()
  6 
  7 b = open("shotcut/TIM图片20180608090653.png",mode="rb")
  8 content = b.read()
  9 print(content)
 10 b.close()
 11 
 12 f = open("name",mode = "r",encoding="UTF-8")
 13 # read可以读取全部内容,但对内存要求较大,可以在括号内选择读几个字节,用数字表示
 14 content = f.read(4)
 15 print(content)
 16 f.close()
 17 
 18 # 读出字节 rb
 19 f = open("name",mode = "rb")
 20 content = f.read(5)
 21 print(content)
 22 f.close()
 23 
 24 # 相对路径练习,相对路径在项目中用的多,可以脱离程序的
 25 f = open("../hello/name1",mode="r",encoding="utf-8")
 26 content = f.read()
 27 print(content)
 28 f.close()
 29 
 30 # readline() 一次读取一行
 31 f = open("sexlady",mode = "r",encoding="UTF-8")
 32 content = f.readline().strip()
 33 content1 = f.readline().strip()
 34 print(content)
 35 print(content1)
 36 f.close()
 37 
 38 # 一次打开文件,读取多行,放在一个列表中,每一行作为一个元素
 39 f = open("sexlady",mode="r",encoding="utf-8")
 40 lst = f.readlines()
 41 for line in lst:
 42     print(line.strip())
 43 f.close()
 44 
 45 # f 打开的文件句柄,是个可迭代对象,可以按行进行迭代输出
 46 # 而在实际工作中多用这种方法,可以节省内存,不必一次全部读取
 47 f = open("sexlady",mode = "r",encoding="utf-8")
 48 for line in f:
 49     print(line)
 50 f.close()
 51 
 52 f = open("song",mode = "w",encoding="utf-8")
 53 # 加上\n之后可在文件中换行写
 54 f.write("hello world!\n")
 55 f.write("this is for testing!")
 56 f.close()
 57 
 58 # 在写的模式下会删除原来的文本,重新写入新的
 59 f = open("song",mode = "w",encoding="UTF-8")
 60 # io.UnsupportedOperation: not readable
 61 # 在只读模式下打开文件如果去读会报错
 62 # content = f.read()
 63 # print(content)
 64 s = ["my love","beat it"]
 65 for e in s:
 66     f.write(e)
 67 f.close()
 68 
 69 # 如果以字节方式写入文件,而又以encoding方式读取文件,会与写的输入一致
 70 f = open("歌单", mode="wb")
 71 f.write("金毛狮王".encode("UTF-8"))
 72 f.flush()
 73 f.close()
 74 
 75 f = open("sexlady",mode = "a",encoding="utf-8")
 76 # 每次只能写入一个字符串,不能够直接在括号内写两个字符串
 77 f.write("龙泽罗拉\n")
 78 f.flush()
 79 f.close()
 80 
 81 f = open("sexlady",mode="r+",encoding="utf-8")
 82 # write
 83 f.write("blabla")
 84 # read
 85 # 读写模式必须先读才能写,如果先写在读是读不到刚写上去的内容的
 86 # 并且是在文件开头进行的覆盖写
 87 content = f.read()
 88 print(content)
 89 f.flush()
 90 f.close()
 91 
 92 f = open("sexlady",mode="r+",encoding="utf-8")
 93 content = f.read()
 94 print(content)
 95 # 在文件的结尾进行追加写,因为文件的光标在读操作后已经移到了最后
 96 f.write("尤美")
 97 f.flush()
 98 f.close()
 99 
100 # 正常情况下如果文件在读操作后,光标不移动到文件的开头的话
101 # 再次进行读操作后是什么都读不出来的
102 f =open("sexlady",mode = "r+",encoding="utf-8")
103 content = f.read() # 光标走到了最后
104 f.seek(0) # 光标回到起始位置
105 c = f.read()
106 print(content)
107 print(c)
108 f.close()
109 
110 # 利用seek方法移动光标位置
111 f =open("sexlady",mode = "r+",encoding="utf-8")
112 f.seek(3) # 移动的是字节单位,在UTF-8中,如果是中文必须移动3的倍数才可以
113 # 返回文件起始位置
114 # f.seek(0)
115 # 移动光标到文件末尾,什么内容都读不出来
116 # f.seek(0,2)
117 content = f.read()
118 print(content)
119 f.close()
120 
121 f = open("sexlady", mode="r+", encoding="utf-8")
122 f.read(2)  # 2代表的是字符
123 print(f.tell())
124 f.seek(6)
125 f.write("fuck")
126 f.flush()
127 f.close()
128 
129 f = open("sexlady", mode="r+", encoding="utf-8")
130 f.seek(15)
131 # 如果没有参数. 根据光标的位置. 保留光标前面的内容.后面的内容删除
132 f.truncate()
133 # 如果给了参数. 会把文件从头到参数之间的内容保留.其他的全部删掉
134 f.truncate(9)
135 f.flush()
136 f.close()
137 
138 # 可以不关文件,过一段时间自动给你关
139 with open("sexlady",mode="r+",encoding="utf-8") as f:
140     content = f.read()
141     print(content)
142 
143 # python对文件中的内容进行修改,并更改文件名
144 import os
145 with open("歌单",mode = "r",encoding="utf-8") as f1,\
146     open("新歌单",mode="w",encoding="utf-8") as f2:
147     content = f1.read()
148     content = content.replace("打篮球","学习")
149     f2.write(content)
150 os.remove("歌单")
151 os.rename("新歌单","歌单")
152 
153 import os
154 with open("歌单",mode = "r",encoding="utf-8") as f1,\
155     open("新歌单",mode="w",encoding="utf-8") as f2:
156     # content = f1.read()
157     # content = content.replace("学习","打篮球")
158     # f2.write(content)
159     for line in f1:
160         line = line.replace("学习","打篮球")
161         f2.write(line)
162 os.remove("歌单")
163 os.rename("新歌单","歌单")

猜你喜欢

转载自www.cnblogs.com/bug-ming/p/9157609.html
今日推荐