10文件操作

  1 """
  2 文件操作
  3 """
  4 # file = open(r"e:123a.txt")  # 默认的只读模式,r
  5 # content = file.read()
  6 # print(content)
  7 # file1 = open("e:/123/a.txt")
  8 # content1 = file1.read()
  9 # print(content)
 10 
 11 """
 12 文件路径在python中的两种写法:
 13 1.r"e:123a.txt"
 14 2."e:/123/a.txt"
 15 路径分两种:
 16 1.绝对路径,从盘符开始到目标文件所在位置的路径
 17 2.相对路径,从当前位置到目标文件所在位置的路径
 18 """
 19 # # 绝对路径,打开当前项目的abc.txt文件
 20 # file2 = open(r"C:UsersdellDesktoppya.txt","r")
 21 # content2 = file2.read()
 22 # print(content2)
 23 # # 相对路径,打开当前项目的abc.txt文件
 24 # file3 = open("a.txt", "r")
 25 # content3 = file3.read()
 26 # print(content3)
 27 # # 我们以后的操作用的都是相对路径,为了方便操作
 28 # # 把目标文件移到了项目中
 29 """
 30 一.打开文件:
 31 open("文件路径以及文件名", "r"/"w"/"a")
 32 1.1)文件路径:(path)
 33     绝对路径:从盘符开始到文件所在位置的路径
 34     相对路径:相对于当前文件夹,目标文件所在的位置的路径。(文章代码演示的都是相对路径)
 35 2.打开模式
 36 "r":默认。只读,文件必须存在
 37 "w":只写,文件如果存在就覆盖,不存在就新建。
 38 "a":追加,文件如果存在就在后面写入,不存在就新建。
 39 加上"b":二进制形式
 40 加上"+":增加读或写的功能
 41 二.关闭文件
 42 关闭文件:变量名.close()
 43 
 44 查看文件的关闭状态:变量名.closed   结果为True (文件已关闭) /  False(文件未关闭)
 45 三.写入文件内容,注意:文件可写入的模式打开。r+,w,w+,a,a+等模式
 46 变量名write("内容")
 47 每使用一次write方法写入内容,新的内容都会追回到原内容的后边
 48 
 49 四.读取文件内容,三种方法:
 50 1.read(size) size省略时,代表读取文件全部内容,size表示一次读取文件的字节数
 51 2.readline() 一次读取文件一行内容
 52 3.readlines() 文件内容以行为单位全部读取出来,以列表形式进行保存
 53 
 54 """
 55 # "a.txt",相对路径
 56 # file = open("a.txt", "r")  # r:以只读模式打开文件,文件必须存在,否则会报错
 57 # file_1 = open("b.txt")  # FilenotfoudError:[Errno 2] No such or directory: 'b.txt'
 58 # # cmd.exe
 59 # file.close()
 60 # # file_1.close()
 61 # print(file.closed)
 62 # print(file_1.closed)
 63 
 64 # file_1 = open("c.txt", "w")  # w模式打开文件,文件不存在,创建一个新文件
 65 # # w模式如果文件存在,信内容会覆盖原内容。
 66 # file_1.write("today is rain, very cool.add clothes.
")
 67 # file_1.write("today is rain, very cool.add clothes.
")
 68 # file_1.write("today is rain, very cool.add clothes.
")
 69 # file_1.close()
 70 # file_1 = open("c.txt")
 71 # content = file_1.read()
 72 # print(content)
 73 
 74 # file = open("c.txt")
 75 # print(file.read(10))  # 读取10个字节的内容
 76 # print(file.readline())  # 读取一行的内容
 77 # content = file.readlines()  # 读取多行的内容
 78 # print(content)
 79 # i = 1
 80 # for temp in content:  # 遍历输出的每行内容
 81 #     print(i,temp)
 82 #     i += 1
 83 # file.close()
 84 # print(file.closed)
 85 
 86 """
 87 题目要求:制作student.txt 的备份文件 student[备份].txt
 88 """
 89 # # 1.获取要备份的文件
 90 # file = input("place input copy filename:")  # input接收的数据类型是字符串
 91 # # 2.根据原文件名生成备份文件
 92 # index = file.rfind(".")  # 在原文件名中查找文件名与扩展名的分隔符,的索引下标
 93 # if index > 0:  # 字符串的rfind方法,在字符串中从右开始查找子串,并返回其下标,未找到-1
 94 #     new = file[:index] + "[备份]" + file[index:]
 95 # else:
 96 #     new = file + "[备份]"  # 没找到
 97 # # 3.从原文件中读取内容写入到目标文件中
 98 # old_file = open(file, "r")  # 以只读找开原文件
 99 # new_file = open(new, "w")  # 以只写w模式找开目标文件,文件不存在创建一个新的
100 # for temp in old_file.readlines():  # 从原文件中读数据
101 #     new_file.write(temp)  # 写入目标文件
102 # # 4.关闭两个文件
103 # old_file.close()
104 # new_file.close()
105 
106 """
107 获取文件读写位置:
108 1.tell()  使用tell方法来获取文件当前读写位置,返回文件的当前位置
109           即文件当前位置,文件下标从0开始
110 2.seek()  使用seek方法来获取文件当前的读写位置
111   seek(offset, from)方法包含两个参数:
112     offset:表示偏移量,也就是代表需要移动偏移的字节数
113     from:表示方向,可以指定从哪个位置开始偏移
114       0.表示文件开头 (默认值)
115       1.表示当前位置 (只能跳0).文件以二进制形式打开,可以跳非0
116       2.表示文件末尾 (只能跳0).文件以二进制形式打开,可以跳非0
117       若要相对当前读写位置或文件末尾进行位移操作,需以二进制形式打开文件
118 """
119 # file = open("student.txt")
120 # print(file.tell())  # 查看文件指针位置,文件打开,指针在前始,下表为0的位置
121 # content_1 = file.read(5)  # 读取5个字节的内容
122 # print(content_1)
123 # print(file.tell())  # 5
124 # content_2 = file.read(10)
125 # print(content_2)
126 # print(file.tell())  # 15
127 # print(file.read(1))  # s
128 # file.seek(0,1)  # 从当前位置偏移0个字节
129 # print(file.read(1))
130 # file.seek(0,0)  # 从文件开始的位置偏移0个字节,回到文件开始位置
131 # print(file.read(5))
132 # print(file.tell())  # 查看指针位置
133 # file.seek(5,0)  # 从文件开始位置偏移5个字节
134 # print(file.seek(0,1))  # 查看指针位置 == tell()
135 # print(file.seek(0,2))  # 158  指针跳到文件末尾位置(最后一个字节的后边)
136 # print(file.read(1))
137 # # file.seek(5,1)  # io.UnsupportedOperation: can't do nonzero cur-relative seeks
138 # # file.seek(5,2)  # io.UnsupportedOperation: can't do nonzero end-relative seeks
139 # file.close()
140 
141 # file = open("student.txt", "rb+")  # 以二进制形式可读写,打开文件
142 # print(file.read(15))  # 读的文件从0-14, 15个字节
143 # print(file.tell())  # 15
144 # print(file.readline())  # 读一行
145 # print(file.tell())  # 指针在60
146 # file.seek(-15,1)  # 在当前位置往回走15个字节
147 # print(file.tell())
148 # print(file.readline(15))
149 # file.seek(-10,2)  # 文件末尾位置回退10个字节
150 # print(file.read())  # 从指针当前位置读到最后
151 # file.seek(5,2)  # 文件末尾位置向后偏移5个
152 # print(file.read())  # 文件末尾向后已经没有内容了,所以读出空字符串
153 # file.close()
154 
155 """
156 文件的重命名和删除:
157 1.文件的重命名:  rename("原文件名", "新文件名")  # 原文件名要给出路径,新文件名也要给出路径
158 2.删除文件: remove("要删除的文件名")
159 """
160 import os
161 
162 # os.rename("student.txt", "AI_student.txt")  # 重命名
163 # os.remove("a.txt")  # 删除文件
164 
165 """
166 文件夹(目录)的相关操作: import os 导入系统模块
167 1.mkdir()   创建目录
168 2.getcwd()  获取当前目录
169 3.listdir()  获取目录列表
170 4.rmdir()   删除文件夹
171 """
172 # os.mkdir("111")
173 # os.mkdir("111/111_1")
174 # os.mkdir("d:python/111")
175 # os.rename("111/111_1/1.txt", "111/111_1/11.txt")
176 # print(os.getcwd())  # C:UsersdellDesktoppy  当前目录
177 # os.chdir("../")  # 改变当前目录为父目录
178 # print(os.getcwd())  # 查看当前目录
179 # # os.mkdir("aaa")
180 # # 查看当前目录下的子目录和文件,目录名,文件名以字符串的形式保存在列表中
181 # print(os.listdir("./"))
182 # print(os.listdir("/users"))
183 # os.chdir("py")  # 查看当前目录为原目录下的py目录
184 # # print(os.listdir("d./"))  # 查看d盘根目录下的子目录和文件
185 # print(os.listdir("./"))  # 查看py目录下的子目录和文件
186 # # ['1.基础语法.py', '111', '3循环.py', '4字符串.py', '5元组.py', '5列表.py', '5字典.py', '5集合.py', '6函数.py', '7高级函数.py', '8文件操作.py', 'b.txt', 'c.txt', 'student.txt', 'student[备份].txt', '作业调试.py', '函数__学生管理系统.py', '函数__手机销售系统.py', '第一次月考机试题__智能游戏机.py']
187 # list_1 = os.listdir("./")
188 # for temp in list_1:
189 #     print(temp)
190 # os.remove("111/111_1/11.txt")  # 先把文件删掉
191 # 再删除目录
192 # os.rmdir("111/111_1")  #     os.rmdir("111/111_!")  # OSError: [WinError 145] 目录不是空的。: '111/111_!'
193 # 56.从键盘输入一个字符串,将小写字母全部转换成大写字母,
194 # 然后输出到一个磁盘文件"test.txt"中保存。
195 # str_1 = input("请输入一个英文字符串:")
196 # file = open("test.txt", "w")
197 # file.write(str_1.upper())
198 # file.close()
199 
200 # 57.有两个磁盘文件A和B,各存放一行字母
201 # 要求把这两个文件中的信息合并(按字母顺序排列),
202 # 输出到一个新文件C中。
203 # file_1 = open("a.txt")   # 以只读模式打开a.txt文件
204 # file_2 = open("b.txt")   # 以只读模式打开b.txt文件
205 # content_1 = file_1.read()   # a文件中的内容
206 # content_2 = file_2.read()   # b文件中的内容
207 # content_3 = content_1 + content_2   # 连接两个字符串
208 # list_1 = list(content_3)   # 将字符串转为列表
209 # list_1.sort(reverse=True)   # 对列表进行排序  空格换行在前边,进行降序
210 # # content_4 = str(list_1)   # 将排序后的列表转换为字符串包含
211 # content_4 = "".join(list_1)   # 使用字符串内建方法join连接列表中的每个元素
212 # file_3 = open("c.txt", "w")
213 # file_3.write(content_4)
214 # file_1.close()
215 # file_2.close()
216 # file_3.close()
217 
218 # 59.当前目录下有一个文本文件test.txt,
219 # 其内容包含小写字母和大写字母。
220 # 请将该文件复制到另一文件test_copy.txt,
221 # 并将原文件中的小写字母全部转换为大写字母,其余格式均不变。
222 # file_1 = open("test.txt")
223 # file_2 = open("test_copy.txt", "w")
224 # content = file_1.readlines()
225 # for temp in content:
226 #     file_2.write(temp.upper())
227 # file_1.close()
228 # file_2.close()
229 
230 # 60.读程序写结果
231 # file = open("test.txt")
232 # content = file.read(6)
233 # content = file.readline()
234 # print(content)
原文地址:https://www.cnblogs.com/zh1127487137/p/12552797.html