08、python的基础--->文件操作

注:用btyes方式的一般都是在“非文字类的(比如图片)”

1、文件的读取

>>>第1种

#绝对路径
s = open('E:\天气.txt', mode='r', encoding='utf-8')      # 用“utf-8 的方式去读取文件内容”,(绝对路径E:\天气.txt)
content = s.read()
print(content)
s.close()

#相对路径
s = open('天气', mode='r', encoding='utf-8')      # 用“utf-8 的方式去读取文件内容”,(相对路径天气-->当前目录下创建)
content = s.read()          # read把数据类型转换   bytes--->str
print(content)
s.close()
View Code

>>>第2种

# 用bytes 的方式去读(b'\xe4\xbb\x8a\xe5\xa4\xa9\xe4\xb8\x8b\xe5\xa4\xa7\xe9\x9b\xa8\xe4\xba\x86\xe3\x80\x82')
s = open('天气', mode='rb')
content = s.read()
print(content)
s.close()
View Code

2、文件的写入

>>>1、对于w:没有此文件就会创建新文件

s = open('天气1', mode='w', encoding='utf-8')    # 新创建一个文件'天气1',并且写入了'全湿透了'
s.write('全湿透了')
s.close()
View Code

>>>2、对于已有文件就会直接删除内容,再重新写入内容

s = open('天气1', mode='w', encoding='utf-8')
s.write('怎么办')
s.close()
View Code

>>>3、用btyes方式写入

s = open('天气1', mode='wb')
s.write('快出太阳吧'.encode('utf-8'))
s.close()
View Code

3、追加功能(只能进行一次追加,不能读取)

>>>第1种:

s = open('天气1', mode='a', encoding='utf-8')       # mode = ‘a'   (全湿透了快回家换衣服)
s.write('快回家换衣服')
s.close()
View Code

>>>第2种:用btyes方式追加

s = open('天气1', mode='ab')
s.write('快回家换衣服'.encode('utf-8'))              # (全湿透了快回家换衣服快回家换衣服)
s.close()
# a+ 可以进行追加之后读取
s = open('天气1', mode='a+', encoding='utf-8')       # mode = ‘a'   (全湿透了快回家换衣服)
s.write('快回家换衣服')
s.seek(0)        # 将光标挪到最前面
print(s.read())
s.close()
View Code

4、文件的读写(只能读-写两步,没办法再读再写(功能与追加类似))

s = open('天气', mode='r+', encoding='utf-8')
content = s.read()          # 先读后写
s.write('全湿透了')        # 先读之后,光标到最后,再写入内容(今天下大雨了全湿透了)
s.close()
# btyes读写
s = open('天气1', mode='r+b')
print(s.read())
s.write('AAAa'.encode('utf-8'))
s.close()
View Code

5、文件的写读(先清除内容再写入)

s = open('天气1', mode='w+', encoding='utf-8')
s.write('AAA')       # 先写,写之前光标在前,把内容覆盖,再从光标位置往后读取内容(一般光标后面无数据)
s.seek(0)            # seek()调光标位置,再读取   (AAA)
print(s.read())
s.close()
View Code

6、功能详解

>>>文件内容(今天下大雨了全湿透了)

s = open('天气', mode='r+', encoding='utf-8')
content = s.read(3)        # read 直接读取字符
print(content)            # 输出 "今天下"
s.close()
View Code
s = open('天气', mode='r+', encoding='utf-8')
s.seek(3)              # seek 按照字节去定光标,一个中文三个字节,只能按照3 的倍数去读
content = s.read()
print(content)         # 输出:天下大雨了全湿透了
print(s.readable())      # 判断是否可读
line = s.readline()       # 一行一行的去读内容
line = s.readlines()       # 以一个列表形式全部输出,每一行当做列表的一个元素(可用for循环打印出来)
print(line)
for line in s:              # 将内容一行一行循环打印出来
    print(line)
s.truncate(4)          # 对原文件内容进行按字节截取
s.close()
View Code

>>>使用循环打印方式

with open('天气', mode='r+', encoding='utf-8') as s:     # 直接按行循环打印出内容,无需close(),自动关闭
    print(s.read())
with open('天气', mode='r+', encoding='utf-8') as s,\
        open('天气', mode='w+', encoding='utf-8') as f:    # 可以同时打开多个文件进行操作
View Code

>>>光标检测

s = open('天气', mode='r+', encoding='utf-8')
s.seek(3)
print(s.tell())    # 检测光标位置,应用:断点续传
s.close()
View Code

----->小程序

# 天气1内容:快回家里
s = open('天气1', mode='a+', encoding='utf-8')
s.write('换大衣服')
count = s.tell()       # 先找到光标位置
s.seek(count-9)        #将光标诺往前9个字节即三个字符,然后读取
print(s.read())        # 输出:大衣服
s.close()
View Code

猜你喜欢

转载自www.cnblogs.com/xiegf87/p/12055734.html