Python-part7-文件

1 什么是文件

文件是操作系统为用户/应用程序提供的一种操作硬盘的抽象单位

2 为何要用文件

用户/应用程序对文件的读写操作会由操作系统转换成具体的硬盘操作
所以用户/应用程序可以通过简单的读\ 写文件来间接地控制复杂的硬盘的存取操作
实现将内存中的数据永久保存到硬盘中
user=input('>>>>: ') #user="egon"

3 如何用文件

文件操作的基本步骤:
f=open(...) #打开文件,拿到一个文件对象f,f就相当于一个遥控器,可以向操作系统发送指令
f.read() # 读写文件,向操作系统发送读写文件指令
f.close() # 关闭文件,回收操作系统的资源

上下文管理:

with open(...) as f:
pass

open(r'D:\SH_fullstack_s3\day07\a.txt')
f=open(r'a.txt',encoding='utf-8') #向操作系统发送请求,要求操作系统打开文件

print(f) # f的值是一个文件对象
print(f.read())

f.close() # 向操作系统发送请求,要求操作系统关闭打开的文件
print(f)
f.read()

强调:一定要在程序结束前关闭打开的文件

上下文管理with

  with open(r'a.txt',encoding='utf-8') as f,\
          open('b.txt',encoding='utf-8') as f1:
      print(f.read())
      print(f1.read())

可读可写:

r+t

w+t

a+t

with open('a.txt',mode='r+t',encoding='utf-8') as f:
    # print(f.readable())
    # print(f.writable())
    msg=f.readline()
    print(msg)
    f.write('xxxxxxxxxxx\n')

with open('c.txt','r+t',encoding='utf-8') as f:
f.seek(13,0)
# f.write('xxx')
f.write('h')

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)

修改文件的方式一:

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')

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

方式二:
优点: 同一时刻在内存中只存在源文件的一行内容,不会过多地占用内存
缺点: 在文件修改的过程中会出现源文件与临时文件共存,硬盘上同一时刻会有两份数据,即在修改的过程中会过多的占用硬盘,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,203评论 2 33
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,877评论 0 38
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,801评论 0 10
  • 魔鬼提问主题。开展交谈的提问。 今天有什么高兴的事儿吗? 你是做什么工作的? 这份工作最吸引你的一点是什么? 有什...
    志坚Ken阅读 295评论 0 0
  • 名称、年代】一刀平五千·新莽 【尺寸、重量】高75.4毫米,厚4.2-5.3毫米,重29.4克 【描述】锉金完整,...
    清语宛如阅读 182评论 0 0