打开文件的模式有:
1. 只读模式(默认)
2. 只写模式(不可读,不存在则创建,存在则覆盖)
3. 追加模式(可读,不存在则创建,存在则只追加内容)

"+"表示可同时读写某个文件:
1. r+可读写文件(可读,可写,可追加)
2. w+写读
3. a+追加

"U"表示在读取时,可以将\r \n \r\n自动转换成\n(与r或者r+模式同时使用)
因为Windows系统的换行符为\r\n,Linux系统的换行符为\n,加上U则能自动把\r\n转换成\n
1. rU
2. r+U

"b"表示处理二进制文件
1.rb
2.wb
3.ab

常用API

#打开文件open()
f = open('test.txt','r+')
#或者with open() 这种方法操作完成后,会自动关闭不需要close()
with open('test.txt','r') as f:
   f.read()


#关闭文件
f = open('test.txt','r+',encoding='utf-8')
ret = f.read()
print(ret)
f.close()


#读取文件内容(可指定每次读取字字符)
f = open('test.txt','r+',encoding='utf-8')
ret = f.read(8)
print(ret)



#读取数据(可指定读取字符数),存为list显示
f = open('test.txt','r+',encoding='utf-8')
ret = f.readlines()
print(ret)
f.close()



#读取一行数据
f = open('test.txt','r+',encoding='utf-8')
ret = f.readline()
print(ret)
f.close()



#写入文件write()参数是字符串
f = open('test.txt','a+',encoding='utf-8')
f.write("abc")
ret = f.read()
print(ret)
f.close()



#写入文件,writelines()参数是序列,比如列表,它会迭代帮你写入文件
f = open('test.txt','a+',encoding='utf-8')
f.writelines(["aa","bb","cc"])
ret = f.read()
print(ret)
f.close()



#判断文件是否是统一tty设备
f = open('test.txt','r+',encoding='utf-8')
ret = f.isatty()
print(ret)  #False
f.close()



#判断是否可读(不可读则报错" No such file or directory: ")
f = open('test.txt','r+',encoding='utf-8')
ret = f.readable()
print(ret)  #True
f.close()



#指定文件中指针的位置
f = open('test.txt','r+',encoding='utf-8')
ret = f.read(8)     #先读取8个字符
print(ret)
f.seek(0)           #然后把指针移动到文件开头处
ret = f.read(8)     #在重新读取
print(ret)
f.close()



#获取指针位置
f = open('test.txt','r+',encoding='utf-8')
ret = f.read(8)     #先读取8个字符
print("pointer position:%s"%f.tell())     #查看当前指针位置
print(ret)
f.seek(0)           #重置指定到启始位
print("pointer position:%s"%f.tell())     #在查看指针位置
f.close()



#截断文件数据,仅保留指定之前数据(指定字节数)
f = open('test.txt','r+',encoding='utf-8')
f.truncate(8)   #文件只保留前8个字节数据,文件后面数据的全部删除
ret = f.read()
print(ret)
f.close()



#文件描述符
f.fileno()



#刷新文件内部缓冲区
f.flush()

 

# 绝对路径
f = open('d:\模特主妇护士班主任.txt', mode='r', encoding='UTF-8')
content = f.read()
print(content)
f.close()

# bytes ---->str
f = open('模特主妇护士班主任', mode='r', encoding='utf-8')
content = f.read()
f.write('fjsdlk')
f.close()

f = open('模特主妇护士班主任', mode='rb', )
content = f.read()
print(content)
f.close()
f = open('log', mode='r+', encoding='utf-8')
print(f.read())
f.close()

f = open('log', mode='r+b')
print(f.read())
f.write('大猛,小孟'.encode('utf-8'))
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()

f = open('log', mode='w+', encoding='utf-8')
f.write('aaa')
f.seek(0)
print(f.read())
f.close()


# f = open('log',mode='wb')
# f.write('附近看到类似纠纷'.encode('utf-8'))
# f.close()

# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()
#
# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()



# f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# f.seek(0)
# print(f.read())
# f.close()


# f = open('log',mode='ab')
# f.write('佳琪'.encode('utf-8'))
# f.close()


# 功能详解

# obj = open('log',mode='r+',encoding='utf-8')
# content = f.read(3)  # 读出来的都是字符
# f.seek(3)  # 是按照字节定光标的位置
# f.tell() 告诉你光标的位置
# print(f.tell())
# content = f.read()
# print(content)
# f.tell()
# f.readable()  # 是否刻度
# line = f.readline()  # 一行一行的读
# line = f.readlines()  # 每一行当成列表中的一个元素,添加到list中
# f.truncate(4)
# for line in f:
#     print(line)
# f.close()







# f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# count = f.tell()
# f.seek(count-9)
# print(f.read(2))
# f.close()

# with open('log',mode='r+',encoding='utf-8') as f,\
#         open('log',mode='w+',encoding='utf-8') as f1: