python--文件操作指南

'''
1. 什么是文件:
文件是操作 系统给用户/应用程序操作硬盘的一种虚拟的概念/接口
用户/应用程序
操作系统(文件)
计算机硬件(硬盘)
2. 为何要用文件
用户/应用程序可以通过文件将数据永久保存的硬盘中,即操作文件就是操作硬盘
用户/应用程序直接操作的是文件,对文件进行的所有的操作都是在向操作系统调用,然后再由操作将其转换成具体的硬盘操作
3. 如何使用文件 open()
    控制文件读写内容的模式
    t和b不能单独使用,必须与r/w/a连用
    t文本 (默认的模式)1读写都以str(unicode)为单位的;2.文本文件 3.必须指定encoding='utf-8'
    b二进制/bytes
    控制文件读写操作的模式
    r:只读模式
    w:只写模式
    a:只追加写模式
    +:r+、w+、a+
'''
'''
操作文件'''
# 1. 打开文件
# f=open(r'aaa/a.txt',mode='rt') #f的值是一种变量,占用的是应用程序的内存空间
# #print(f) #<_io.TextIOWrapper name='aaa/a.txt' mode='r' encoding='UTF-8'>
# # 2.操作文件,读/写文件
# res=f.read()
# print(res)
# # 3. 关闭文件
# f.close()   #回收操作系统资源
# #del f       #回收应用程序资源
# print(f)
# f.read()  #ValueError: I/O operation on closed file.
'''with 语法'''
# with open(r"aaa/a.txt",mode='wt',encoding='utf-8') as f1,\
#         open(r'aaa/b.txt') as f2:
# #    res1=f1.read()  #解码:windows默认是gbk,linux utf-8,
# #    res2=f2.read()
# #    print(res1,res2)
#     res1=f1.write("哈哈哈")
#     print(res1)
# ''''''

# username='dandan'
# password='123'
# inp_username=input("please input your name: ").strip()
# inp_password=input("please input your password: ").strip()
# if inp_username==username and inp_password==password:
#     print("you login successfully!")
# else:
#     print("the name or password is incorrect!")

# inp_username = input("please input your name: ").strip()
# inp_password = input("please input your password: ").strip()
# with open(r"aaa/user.txt",mode='rt',encoding='utf-8') as f1:
#     for line in f1:
# #        print(line,end='')
#         username,password=line.strip().split(':')
# #        print(line)
#         if inp_username==username and inp_password==password:
#             print("you login successfully!")
#             break
#     else:
#         print("the name or password is incorrect!")
'''w :不建议用打开,一打开就清空啦'''
# with open("aaa/c.txt",mode='wt',encoding='utf-8') as f:
#     f.write("wu~~~\n")

#以w模式打开文件没有关闭的情况下,连续的写,新的内容总在旧的后面
# with open("aaa/c.txt",mode='wt',encoding='utf-8') as f:
#     f.write("wu~~~\n")
#     f.write("hahahah\n")
#     f.write("第三行\n")
#重新以w模式打开文件,则会清空文件内容
# with open("aaa/c.txt",mode='wt',encoding='utf-8') as f:
#     f.write("1.wu~~~\n")
# with open("aaa/c.txt",mode='wt',encoding='utf-8') as f:
#     f.write("2.hahaha~~~\n")
# with open("aaa/c.txt",mode='wt',encoding='utf-8') as f:
#     f.write("3.666~~~\n")

'''a:只追加写,在文件不存在的时候'''
# with open("aaa/d.txt",mode='at',encoding='utf-8') as f:
# #    f.read() #io.UnsupportedOperation: not readable
#      f.write("开始写内容了")
# with open("aaa/d.txt",mode='at',encoding='utf-8') as f:
# #    f.read() #io.UnsupportedOperation: not readable
#      f.write("\n2。之前的内容还在")
#相同点:在打开的文件不关闭的情况下,连续的写入,新写的内容总会在前写的后面
#不同点:以a模式重新打开文件,不会清空原文件内容,会将文件指针直接到文件的末尾
#一般是用来记录日志
#a 模式用来在原有内容的基础上进行追加,例如用户注册功能
# name=input("please input your name: ")
# pwd=input("please input your password: ")
# with open("aaa/user.txt",mode="at",encoding='utf-8') as f:
#     f.write("{}:{}\n".format(name,pwd))

"""w 用作创建全新的文件 """
##文件的copy工具:
# with open("aaa/user.txt",mode="rt",encoding="utf-8")as f1,\
#     open("aaa/user01.txt",mode="wt",encoding="utf-8")as f2:
#     res=f1.read()
#     f2.write(res)

# src_file=input("源文件文件:")
# dst_file=input("目标文件:")
# with open(r"{}".format(src_file),mode="rt",encoding="utf-8")as f1,\
#     open(r"{}".format(dst_file),mode="wt",encoding="utf-8")as f2:
#     res=f1.read()
#     f2.write(res)

'''b 模式 binary 模式,读写都是以bytes 为单位,可以针对所有文件,*一定不能指定utf-8'''
# with open(r"aaa/WechatIMG4.jpeg",mode='rb') as f:
#     res=f.read() #硬盘的二进制读入内存->b模式下,不做任何转换,直接读入内存
#     print(res) #byte类型-->当成二进制

'''指针移动的单位都是以bytes/字节为单位
只有一种特殊情况:t模式下的read(n),n 代表的是字符个数'''

# with open(r'aaa/d.txt',mode='rt',encoding='utf-8')as f:
#     res=f.read(4)
#     print(res)

# f.seek(n,模式);n指的是移动的字节个数
#模式:
'''0:参照物是文件开头位置'''
# f.seek(9,0)
# f.seek(3,0)  #3
'''1:参照物是当前指针所在位置'''
# f.seek(9,1)
# f.seek(3,1) #12
'''2:参照物是文件的末尾位置,应该是倒着移动'''
# f.seek(-9,2) #3
# f.seek(-3,2) #9
#
# f.tell() #获取当前指针的位置
with open(r"aaa/a.txt",mode='rt') as f:
    f.seek(9,0)
    f.seek(3,0)
    print(f.tell())
    res=f.read()
    print(res)

 

posted @ 2024-04-15 19:52  正霜霜儿  阅读(1)  评论(0编辑  收藏  举报