• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
喵了个喵0507
博客园             管理     
【python】第一模块 步骤二 第六课、文件读写模式

第六课、文件读写模式

一、课程概要

  章节概要

  • 文件读写模式
  • 文件的打开和关闭
  • 文件的读取
  • 文件写入

二、文件的读写模式

  2.1文件的读写模式

  文件读写模式

值 描述
‘r’ 读取模式(默认值)
‘w’ 写入模式
‘x’ 独占写入模式
‘a’ 附加模式
‘b’ 二进制模式(与其他形式结合使用)
‘’t 文本模式(默认值,与其他模式结合使用)
‘+’ 读写模式(与其他模式结合使用)

 

 

 

 

 

 

  文件的打开和关闭

  • 使用open函数打开文件
    • f = open('test.txt')
  • 使用close函数关闭文件
    • f.close()
    • #tips此处要做异常处理
  • 使用with语法
    • with open("somefile.txt") as f:
    •   do_something(f)
  • 到达该语句末尾时,将自动关闭文件,即便出现异常亦如此

  2.2文件的读取

  文件的读取

  • read():读取文件,可以指定参数,表示读几个字符(字节)
  • readline():读取一行数据,可以指定参数,表示读前几个字符(字节)
  • readlines():读取所有行,并返回列表
 1 def read_file():
 2     """读取文件"""
 3     file_name = "test.txt"
 4     """使用绝对路径"""
 5     file_path = 'C:\\Users\\19342\\Desktop\\me\\py_learn\\chapter03\\test.txt'
 6     file_path2 = 'C:/Users/19342/Desktop/me/py_learn/chapter03/test.txt'
 7     # 使用普通的方式来打开文件
 8     with open(file_path2, encoding='utf-8') as f:
 9         # 读文件内容
10         # rest = f.read()
11 
12         # 读取指定的几个内容
13         # rest = f.read(8)
14         # print(rest)
15         # print(f.read(8))
16 
17         # 随机读取
18         # f.seek(10)
19         # print(f.read(3))
20 
21         # 按行读取
22         # rest = f.readline(8)
23         # print(rest)
24         # print(f.readline(8))
25 
26         # 读取所有行
27         rest = f.readlines(2)
28         print(rest)
29         for i in rest:
30             print(i)
31 
32         # 关闭文件
33         # f.close()
34 
35 if __name__ == '__main__':
36     read_file()

 

  2.3文件的写入

  文件写入

  • 使用write函数向打开的文件对象写入内容
  • 使用writelins函数向打开的文件对象写入多行内容
  • 以往后追加的模式写入文件
 1 from datetime import datetime
 2 import random
 3 
 4 
 5 def write_file():
 6     """写入文件"""
 7     file_name = 'write_test.txt'
 8     # 以写入的方式打开文件
 9     f = open(file_name,'w')
10     # 写入一行内容
11     f.write("hello")
12     # 换行符
13     f.write('\n')
14     # 再写入一行内容
15     f.write("world")
16 
17     # 关闭文件
18     f.close()
19 
20 
21 def write_mult_line():
22     """像文件中写入多行内容"""
23     file_name = 'write_mult_line.txt'
24     with open(file_name, 'w', encoding='utf-8') as f:
25         l = ['第一行', '\n', '第二行', '\r', '第三行']
26         f.writelines(l)
27 
28 
29 def write_user_log():
30     """记录用户的日志"""
31     # 记录时间+记录用户的ID
32     rest = '用户:{0}-访问时间:{1}'.format(random.randint(1000, 9999), datetime.now())
33     print(rest)
34     file_name = 'write_user_log.txt'
35     with open(file_name, 'a', encoding='utf-8')as f:
36         f.write(rest)
37         f.write('\n')
38 
39 
40 def read_and_write():
41     """ 先读,再写入 """
42     file_name = 'read_and_write.txt'
43     with open(file_name, 'r+', encoding='utf-8') as f:
44         read_rest = f.read()
45         # 如果里面没有1,就写一行数据,aaa
46         # 如果有1,写入一行数据,bbb
47         if "1" in read_rest:
48             f.write("bbb")
49         else:
50             f.write("aaa")
51         f.write('\n')
52 
53 if __name__ == '__main__':
54     # write_file()
55     # write_mult_line()
56     # write_user_log()
57     read_and_write()

 

三、课程总结

  章节概要

  • 文件读写模式
  • 文件的打开和关闭
  • 文件的读取
  • 文件写入

  知识点回顾

  • 文件读写模式
  • 文件的打开和关闭
  • 文件的读取
    • read():读取文件,可以指定参数,表示读几个字符(字节)
    • readline():读取一行数据,可以指定参数,表示读前几个字符(字节)
    • readlines():读取所有行,并返回列表
  • 文件写入
    • 使用write函数向打开的文件对象写入内容
    • 使用writelines函数向打开的文件对象写入多行内容
    • 以往后追加的模式写入文件

  重点知识

  • 常用的文件读写模式
  • 两种文件打开和关闭的方式
  • 文件的读取和写入

 

posted on 2019-11-13 18:19  喵了个喵0507  阅读(193)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3