lxcai213
# open 参数介绍
#  file  :用来指定打开的文件(不是文件的名字,而是文件的路径)
#  mode  :打开文件时的模式,默认是r表示 只读
#  encoding:打开文件时的编码方式
# file.read()  读取文件
#  file.close() c操作完成文件后,关闭文件
#  tell 告诉    seek  查找      write  写      flush 冲刷 刷新    buffering 刷新
# 
# r\'       open for reading (default)
#     \'w\'       open for writing, truncating the file first
#     \'x\'       create a new file and open it for writing        # 创建一个新文件 并 写
#     \'a\'       open for writing, appending to the end of the file if it exists    #
#     \'b\'       binary mode       #  二进制
#     \'t\'       text mode (default)    #   以文本操作
#     \'+\'       open a disk file for updating (reading and writing)     #     读和写
#     \'U\'       universal newline mode (deprecated)
# 
#  xxx。txt 写入时,使用得utf8编码格式
#  在windows操作系统里,默认使用gdk 编码格式打开文件
#  open 函数会有一个返回值  , 打开的文件对象
# 解决方案 :写入和读取 使用相同的编码格式。
#  写入用什么编码格式写,读取也要同步

  -------# 文件句柄 = open()
file = open("xxx.txt",encoding=\'utf8\')
# print(type(file))
print(file.read())
open(\'xxx.txt\',mode=\'r\',encoding=\'utf8\')
re = open("xxx.txt", mode=\'r\', encoding=\'utf8\')
print(re.read())
re.close()

# readline
f= open(\'miaonan.txt\',mode=\'r+\',encoding=\'utf8\')
content = f.readline().strip()
print(content)
content = f.readline().strip()
print(content)
content = f.readline()
print(content)
t = f.tell()
print(t)
f.close()


 # ----- 写  write
re = open(\'miaonan.txt\',mode=\'w\',encoding=\'utf8\')     #
f = re.write(\'秒男\'+"zhang")                           #  返回字符串得个数
re1 = open(\'miaonan.txt\',mode=\'a\',encoding=\'utf8\')    #  追加写
f1 = re1.write(\'nihao\')
print(f)
re.close()

  # --- r+ w+ 指针   tell  seek
f5 = open(\'miaonan.txt\',mode=\'r+\',encoding=\'utf8\')
re = f5.read(5)
print(re)
t = f5.tell()
print(t)
# f5.seek(5)                     // 改变指针位置
content = f5.read()
print(content)
f5.close()





   # ------  读取一个文件 -写进一个新文件
f2 = open(\'miaonan.txt\',encoding=\'utf8\')      #  读取一个文件内容
f3 = open(\'ooo\',mode=\'w\',encoding=\'utf8\')     #   写一个新文件
content = f2.read()                           #  去读文件内容提取
f3.write(content)                             #  把 读取内容写进新文件里
f2.close()
f3.close()





# -----------文件的路径
# import os
# 
# print(os.name)  # NT/ posix
# print(os.sep)  # windows系统里,文件夹之间使用\ 分隔
# 在python 的字符串里 , \ 表示转义字符
#  在Windows系统里 文件夹使用\分隔
#  在非Windows系统里 文件夹使用 / 分隔
# 
# 路径书写的三种方式: 1. \\  2.r\'\\' 3.\'/\'(推荐使用)
# 
# 路径分为两种:
# 1.绝对路径:从电脑盘符开始
# file = open(\'C:\\Users\\dell\\PycharmProjects\\jh_pyton 学习\\xxx.txt\')
# file = open(r\'C:\Users\dell\PycharmProjects\jh_pyton 学习\xxx.txt\')
# file = open(\'C:/Users/dell/PycharmProjects/jh_pyton 学习/xxx.txt\')
# 
# 2.相对路径:当前文件所在的文件夹开始的路径。
# file = open(\'xxx.txt\')
# # file = open(\'../\')#  ../返回上一级
# print(file.read())



# 读图片写到另一个文件里边
l = open("xxxd.jpg",mode=\'rb\')
re = open(\'bs.png\', mode=\'wb\')
content = l.read()
re.write(content)
print(content)
print(re)
l.close()
re.close()

with open(\'ooo\',mode=\'a+\',encoding=\'utf8\') as f1:     #   便捷打开文件方式
    for  i in f1:
        print(i)

with open(\'xxxd.jpg\',mode=\'rb\') as fl,\
        open(\'xxtt.jpg\',mode=\'wb\')as f2 :
    # content = fl.read()
    # f2.write(content)
    for i in fl:
        f2.write(i)

 

分类:

技术点:

相关文章: