python文件操作

一. 读文件

过程:
打开文件

读文件内容

关闭文件

打开文件方式:

open(path,flag,[encoding [ERRORS]])
path:要打开文件的路径
flag :打开方式

* r 以只读的方式打开文件 文件法人描述符放在开头

* rb 以二进制格式打开一个文件用于只读 文件的描述符放在开头 (二进制用来加密)

r+ 打开一个文件用于读写 文件的描述符放在开头

* w 打开一个文件只用于写入 如果该文件已经存在会覆盖 如果不存在则创建新文件
* wb 打开一个文件只用于写入二进制 如果该文件已经存在会覆盖 如果不存在则创建新文件

* w+ 打开一个文件用于读写

a 打开一个文件用于追加 如果文件存在 文件描述符将会放到文件末尾

a+

encoding 编码格式 常用的的是utf-8
ERRORS 错误处理 

# 打开文件

# 过程:

#    打开文件

#    读文件内容

#    关闭文件







# 打开文件
path=r"D:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")

# 读文件内容
#读取文件里内容一整行  包括换行符 /n                     readline
str1=f.readline()   
print(str1)   # my name is 哈哈哈
# 关闭文件







path=r"E:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")

# 读文件内容
  读取指定字符串    
str1=f.readline(10)   
print(str1)   # my name is 哈哈哈
View Code
# 打开文件

path=r"D:\Studypython\py2\01.txt"

# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")
# 读文件内容
# 读取文件里面10个字符    read(10)

# 读取文件里指定字符数read(n)
str1=f.read(10)   
print(str1)   # my name is

# 关闭文件
View Code
# 打开文件
path=r"D:\Studypython\py2\1\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")
# 读文件内容
# 读取文件里的所有内容    read()
str1=f.read()
print(str1)  

# my name is 哈哈哈
# i lover you to
# 哈哈哈哈啦啦啦
# 关闭文件
View Code
path=r"D:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")
# 读文件内容
# 读取文件里面10个字符    read(10)

# 读取文件里指定字符数read(n)
str1=f.read(10)   
print(str1)   # my name is
# 关闭文件
View Code
path=r"D:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")

# 读文件内容
#读取文件里内容一整行  包括换行符 /n                     readline
str1=f.readline()   
print(str1)   # my name is 哈哈哈
# 关闭文件
View Code
path=r"E:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")

# 读文件内容
  读取指定字符串    
str1=f.readline(10)   
print(str1)   # my name is 哈哈哈
View Code
path=r"D:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")

# 读文件内容
#读取文件里内容 所有行    并返回列表                    readlines
str1=f.readlines()   
print(str1)   # my name is 哈哈哈
#['my name is 哈哈哈\n', '\n', 'i lover you to\n', '\n', '哈哈哈哈啦啦啦']
# 关闭文件
View Code
path=r"D:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")
# 读文件内容
# 若给定的数字大于0 返回实际size节的行数
str1=f.readlines(25)   
print(str1)   
# ['my name is 哈哈哈\n', '\n', 'i lover you to\n']
# 关闭文件
f.close()
View Code

fileObject.seek(offset[, whence])
参数
offset -- 开始的偏移量,也就是代表需要移动偏移的字节数
whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
返回值

path=r"D:\Studypython\py2\01.txt"
# 忽略错误  ignore
# f=open(path,"r",encoding="utf-8",errors="ignore")
f=open(path,"r",encoding="utf-8")
# 读文件内容
# 修改描述符的位置
# seek (str)  表示从第字符开始文件内容
# seek() 方法用于移动文件读取指针到指定位置。
f.seek(10)
str1=f.read()   
print(str1)   
#  哈哈哈
# i lover you to
# 哈哈哈哈啦啦啦
# 关闭文件
f.close()
View Code

# 打开文件读文件的一个完整的过程 方法一

path=r"E:\Studypython\py2\1\01.txt"
try:
    f=open(path,"r",encoding="utf-8")
    str1=f.read()   
    print(str1)   
finally:
    if f:
       f.close()
# my name is 哈哈哈
# i lover you to
# 哈哈哈哈啦啦啦
# 关闭文件
View Code

# 打开文件读文件的一个完整的过程 方法二

# 打开文件读文件的一个完整的过程  方法二
path=r"E:\Studypython\py2\1\01.txt"
with open(path,"r",encoding="utf-8") as f2:
     print(f2.read())

#      my name is 哈哈哈
# i lover you to
# 哈哈哈哈啦啦啦
View Code

 

 二. 写文件

     写文件

path=r"E:\Studypython\py2\1\02.txt"
f=open(path,"w",encoding="utf-8")
# 1 将信息写入缓冲区
f.write("my name is hao do you do")
# 2 刷新缓冲区
#  直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待 自动刷入缓冲区

f.flush()
while True:
    pass
f.close()
View Code  
# 写文件1
import time
path=r"E:\Studypython\py2\1\03.txt"
f=open(path,"w",encoding="utf-8")
# 1 将信息写入缓冲区
# 2 刷新缓冲区
#  直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待 自动刷入缓冲区

# f.flush()
while 1:
     f.write("my name is hao do you doLLLLLLLLLLLLLLLLLLLLLLLLLLLL")
     f.flush()
     time.sleep(0.1)
f.close()
View Code
# 写文件
import time
# 简易写法 写文件    
# 这种写法不用关闭和刷新
path=r"D:\Studypython\py2\1\04.txt"
with open(path,"a",encoding="utf-8")as f2:

    f2.write("哈哈哈哈哈啊哈哈哈哈啊哈哈哈哈哈哈哈哈")
View Code

 写文件二进制编码和解码

path=r"D:\Studypython\py2\1\05.txt"
# 注意编码和解码的字符集要一致
# 写入文件编码
with open(path,"wb")as f2:
    str="my name is haha  heee1s 张三丰"
    f2.write(str.encode("utf-8"))

# 读文件解码
with open(path,"rb") as f3:
   data=f3.read()
   print(data)               #  b'my name is haha  heee1s'  带b的二进制
   print(type(data))         #  <class 'bytes'>             字节类型
   newData=data.decode("utf-8")
   print(newData)
View Code

import pickle  #数据持久性模块

import pickle  #数据持久性模块

# 写入文件
path=r"E:\Studypython\py2\2\01.txt"
mylist=[1,2,3,4,5,6,"sumk is a good man fffffffffffffffffffffffffffffffffffffffffffff"]

f=open(path,"wb")
pickle.dump(mylist,f)
f.close()
# 用于序列化的两个模块
#   json:用于字符串和Python数据类型间进行转换
#   pickle: 用于python特有的类型和python的数据类型间进行转换
#   json提供四个功能:dumps,dump,loads,load
#   pickle提供四个功能:dumps,dump,loads,load

# pickle可以存储什么类型的数据呢?
# 所有python支持的原生类型:布尔值,整数,浮点数,复数,字符串,字节,None。
# 由任何原生类型组成的列表,元组,字典和集合。
# 函数,类,类的实例


# 读取文件
f2=open(path,"rb") 
timelist=pickle.load(f2)
print(timelist)
f2.close()  
# [1, 2, 3, 4, 5, 6, 'sumk is a good man fffffffffffffffffffffffffffffffffffffffffffff']
View Code

 

posted @ 2018-12-21 14:10  Loversuperme  阅读(147)  评论(0编辑  收藏  举报