码迷,mamicode.com
首页 > 其他好文 > 详细

文件的打开方式

时间:2017-08-24 14:53:59      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:txt   des   ble   上传   dmi   添加   write   汉字   关闭   


# 文件(保存文件格式) ---- 打开文件(指定编码格式) ----打开模式


# 读取utf-8编码数据,转换成 unicode(str)编码的数据


# ============================== 只读 ============================

# =====r 打开 需要转换编码格式=====

# file=open(‘D:\\sdas\\sadsad\1.txt‘,encoding="gbk",mode=‘r‘) ====>> 字符串 str


# =====rb 打开 内存中是 bytes =====>>> 上传,下载文件 =====

# file=open(‘D:\\sdas\\sadsad\1.txt‘,mode=‘rb‘) ===>>>> 二进制 bytes


# 1
# path=r‘C:\Users\Administrator\Desktop\1.txt‘
# file=open(path,encoding=‘utf-8‘,mode=‘r‘)
# a=file.read()
# print(a)
# file.close()
# 2
# path=r‘C:\Users\Administrator\Desktop\1.txt‘
# file=open(path,‘rb‘)
# a=file.read().decode(‘gbk‘)
# print(a)
# file.close()

# =============================== 只写 =============================

# -------w------需要解码
# path=r‘C:\Users\Administrator\Desktop\2.txt‘
# file=open(path,mode=‘w‘,encoding=‘utf-8‘)
# file.write(‘adasdad‘)
# file.close()

# --------wb------直接读二进制
# path=r‘C:\Users\Administrator\Desktop\2.txt‘
# file=open(path,‘wb‘)
# file.write(‘哈哈哈‘.encode(‘utf-8‘))
# file.close()

# =========================== 只追加添加 =============================

# -----------a-------------

# path=r‘C:\Users\Administrator\Desktop\3.txt‘
# file=open(path,‘a‘,encoding=‘utf-8‘)
# file.write(‘哈‘)
# file.close()

# -------------ab----------------
# path=r‘C:\Users\Administrator\Desktop\3.txt‘
# file=open(path,‘ab‘)
# file.write(‘哈‘.encode(‘utf-8‘))
# file.close()


# ============================== w+先清空后读 r+先追加 a+ ============================

# ========== r+ 丛光标位置开始 file.seek(1)

# 一个读的光标 一个写的光标


# ------------- r+ 读 和 追加-----------可以调整指针-------------------

# path=r‘C:\Users\Administrator\Desktop\3.txt‘
# file=open(path,‘r+‘,encoding=‘utf-8‘)
# a=file.read()
# print(a)
# file.write(‘哈‘)
# file.close()

# -------------a+-------------不可以调整指针 永远在最后追加----------------

# 默认写的时候永远是在最后写

# ------------ w+-----------先清空后读-----------------------


# ----------------------------------------
# ==============文件操作的功能函数====================
# --------------------------------------

# --------按格式读长度----file.read(2)----r--->字符=====rb--->字节


# ----------------------
#-------seek-->>>> 后面是字节
# ----------------------
# -------指定指针位置------file.seek(2)----字节数----gbk--2个字节一个汉字
# -------指定指针位置------file.seek(3)----字节数----utf-8--3个字节一个汉字

# -------查看当前指针位置------file.tell()----


# ----------------file.writable()---------------------------
# ----------------file.readable()---------------------------


# -------------一般文件操作没有完成硬盘上的时候,数据不会存在----------
# ---------------file.flush()----从内存强刷到硬盘------------------

# ----------------file.readline()- 读一行--------------------------

# ----------------file.truncate()- 截取 指针位置前面的数据--------------------------
#
# ---------------file.readlines()-----读取后全部保存到内存------------

#
# readline() --------- 每行
#
# for line in file:
# print(line.read()) ========>>>>> # python 2 xreadlines()



#===================== 关闭文件操作=========================
#
# with open(r‘C:\Users\Administrator\Desktop\3.txt‘,mode=‘rb‘) as file:
# file.read()
#=================================================================


文件的打开方式

标签:txt   des   ble   上传   dmi   添加   write   汉字   关闭   

原文地址:http://www.cnblogs.com/big-handsome-guy/p/7422829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!