话不多说直接码

# 绝对路径
# f = open(\'/Users/fangxiang/Downloads/我的古诗.text\', mode=\'r\', encoding=\'utf-8\')
# content = f.read()
# print(content)
# f.close()

# 相对路径
 f = open(\'我的古诗.text\', mode=\'r\', encoding=\'utf-8\')
 content = f.read()
 print(content, type(content))
 print(f, type(f))
 f.close()

# 1、打开非文字类文件的读取看
# 2、上传下载储存时
 f = open(\'我的古诗.text\', mode=\'rb\')
 content = f.read()
 print(content)
 f.close()

# 写 w
# 对于写 没有此文件就创建文件
# f = open(\'log\', mode=\'w\', encoding=\'utf-8\')
# f.write(\'高清五码\')
# f.close()
# 先将源文件的内容全部清除,再写
# f = open(\'log\', mode=\'w\', encoding=\'utf-8\')
# f.write(\'绝对好看\')
# f.close()

# # 写wb 以bytes方式写入
# f = open(\'log\', mode=\'wb\')
# f.write(\'附件看到类型节分\'.encode(\'utf-8\'))
# f.close()

# 追加 a

# f = open(\'log\', mode=\'a\', encoding=\'utf-8\')
# f.write(\'追加进去的内容1\')
# f.close()
# # 追加 ab 以bytes方式追加
# f = open(\'log\', mode=\'ab\')
# f.write(\'追加进去的内容2\'.encode(\'utf-8\'))
# f.close()


# 读写 r+ 先把原文章读出来,再追加进去 也有bytes类型 r+b
# f = open(\'log\', mode=\'r+\', encoding=\'utf-8\')
# content = f.read()
# print(content)
# f.write(\',读后追加的内容\')
# f.close()
# r+先写后读(光标从头开始写,再读取光标后的内容)
# f = open(\'log\', mode=\'r+\', encoding=\'utf-8\')
# f.write(\'wer\')
# content = f.read()
# print(content)
# f.close()
# r+b 读写 bytes形式
# f = open(\'log\', mode=\'r+b\')
# content = f.read()
# print(content)
# f.write(\',读后追加的内容\'.encode(\'utf-8\'))
# f.close()

# 写读 w+  写后在读就读不出来了 w+b
# f = open(\'log\', mode=\'w+\', encoding=\'utf-8\')
# f.write(\'先写,厚度\'),
# print(f.read())
# f.close()

# 追加 a+
# f = open(\'log\', mode=\'a+\', encoding=\'utf-8\')
# f.write(\'——这里是追加的内容+\')
# f.seek(0)
# print(f.read())
# f.close()
#  a+b

# 功能详解
# seek()
# f = open(\'log\', mode=\'r+\', encoding=\'utf-8\')
# content = f.read(3)  # 读出来的都是字符 读取三个字符
# print(content)

# f.seek(3)  # 光标是按照字节去找的  是按照字节定光标
# 断点续传
# f.tell()  告诉你光标的位置(字节计算),然后在seek调到该位置


# print(f.tell())
# content = f.read()
# print(content)
# f.close()


# 全查看光标,在编号光标位置,最后读取
# f = open(\'log\', mode=\'a+\', encoding=\'utf-8\')
# f.write(\'——加起\')
# count = f.tell()
# f.seek(count-9) # 读取后三中文字符
# content = f.read()
# print(content)
# f.close()

# f.tell() 光标位置
# f.readable() 判断是否可读
# f.truncate(2)  对原文件进行截取 修改源文件 截取前2个字节
f = open(\'log\', mode=\'r+\', encoding=\'utf-8\')
# f.truncate(3)

# line = f.readline()  # 一行一行的读
# lines = f.readlines()  # 每一行当成列表中的一个元素,添加到list中

# print(line)
# print(lines)
# for line in f:
#     print(line)
# f.close()


# 打开文件方式 不需要关闭文件f.close()

# 方式一
# with open(\'log\', mode=\'r+\', encoding=\'utf-8\') as obj:
#     print(obj.read())


# 方式二
with open(\'log\', mode=\'r+\', encoding=\'utf-8\') as f,\
    open(\'模特主妇护士老师.text\', mode=\'r+\', encoding=\'utf-8\') as f1:
        print(f1.read())
        print(f.read())

  

版权声明:本文为directiones原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/directiones/p/10766856.html