文件操作

# f.seek
# 文件内指针移动,只有t模式下的read(n),n代表的字符的个数
# 除此以外文件内指针的移动都是以字节为单位
# with open('a.txt',mode='rt',encoding='utf-8') as f:
#     msg=f.read(1)
#     print(msg)

# with open('a.txt',mode='rb') as f:
#     msg=f.read(3)
#     print(msg.decode('utf-8'))


# f.seek(offset,whence)有两个参数:
# offset: 代表控制指针移动的字节数
# whence: 代表参照什么位置进行移动
#        whence = 0: 参照文件开头(默认的),特殊???,可以在t和b模式下使用
#        whence = 1: 参照当前所在的位置,必须在b模式下用
#        whence = 2: 参照文件末尾,必须在b模式下用

# with open('a.txt',mode='rt',encoding='utf-8') as f:
#     f.seek(6,0)
#     msg=f.read(1)
#     print(msg)

# with open('a.txt',mode='rb') as f:
#     f.seek(3,0)
#     msg=f.read(3)
#     print(msg.decode('utf-8'))

# with open('a.txt',mode='rb') as f:
#     msg=f.read(3)
#     # print(msg.decode('utf-8'))
#     print(f.tell())
#     # f.seek(6,0)
#     f.seek(3,1)
#     msg1=f.read(3)
#     print(msg1.decode('utf-8'))


# with open('a.txt',mode='rb') as f:
#     msg=f.read(3)
#     # print(msg.decode('utf-8'))
#     print(f.tell())
#     # f.seek(6,0)
#     f.seek(3,1)
#     msg1=f.read(3)
#     print(msg1.decode('utf-8'))


# with open('a.txt',mode='rb') as f:
#     # f.seek(0,2)
#     # print(f.tell())
#     f.seek(-3,2)
#     msg=f.read(3)
#     print(msg.decode('utf-8'))



# with open('access.log',mode='rb') as f:
#     f.seek(0,2) # 当前位置是147bytes
#
#     while True:
#         line=f.readline() # 当前位置是196bytes
#         # print(f.tell())
#         if len(line) == 0:
#             # 没有新的一行内容追加进来
#             pass
#         else:
#             # 有新的一行内容追加进来
#             print(line.decode('utf-8'),end='')


# with open('access.log',mode='rb') as f:
#     f.seek(0,2) # 当前位置是147bytes
#
#     while True:
#         line=f.readline() # 当前位置是196bytes
#         if len(line) != 0:
#             print(line.decode('utf-8'),end='')

with open('a.txt',mode='r+t',encoding='utf-8') as f:
    f.truncate(6)
View Code
# with open('c.txt','r+t',encoding='utf-8') as f:
#     f.seek(13,0)
#     # f.write('在老男孩')
#     f.write('h')

# 修改文件的方式一:
# 1 将文件内容由硬盘全部读入内存
# 2 在内存中完成修改
# 3 将内存中修改后的结果覆盖写回硬盘

# with open('d.txt',mode='rt',encoding='utf-8') as f:
#     all_data=f.read()

# print(all_data,type(all_data))

# with open('d.txt',mode='wt',encoding='utf-8') as f:
#     f.write(all_data.replace('alex','dsb'))


# 错误的做法
# with open('d.txt',mode='rt',encoding='utf-8') as f1,open('d.txt',mode='wt',encoding='utf-8') as f2:
#     all_data=f1.read()
#     f2.write(all_data.replace('dsb','alex'))



# 修改文件的方式二:
# 1 以读的方式打开源文件,以写的方式打开一个临时文件
# 2 从源文件中每读一样内容修改完毕后写入临时文件,直到源文件读取完毕
# 3 删掉源文件,将临时文件重命名为源文件名
# import os
#
# with open('d.txt',mode='rt',encoding='utf-8') as read_f,open('.d.txt.swap',mode='wt',encoding='utf-8') as write_f:
#     for line in read_f:
#         write_f.write(line.replace('alex','dsb'))
#
# os.remove('d.txt')
# os.rename('.d.txt.swap','d.txt')

# 方式一:
# 优点: 在文件修改的过程中硬盘上始终一份数据
# 缺点: 占用内存过多,不适用于大文件


# 方式二:
# 优点: 同一时刻在内存中只存在源文件的一行内容,不会过多地占用内存
# 缺点: 在文件修改的过程中会出现源文件与临时文件共存,硬盘上同一时刻会有两份数据,即在修改的过程中会过多的占用硬盘,
原文地址:https://www.cnblogs.com/xuqidong/p/11939045.html