1.向文本文件中写入内容

s = 'Hello world\n文本文件的读取方法\n文本文件的写入方法\n'
# 需要写入文件的字符串
print('显示需要写入的内容:\n{0:s}'.format(s))
#-----文件操作开始------------
f = open('sample.txt', 'a+') # 以追加(a)和读写(+)的模式打开并创建文件对象f
f.write(s) # 对文件对象f使用write方法
f.close() # 关闭文件
#-----文件操作结束------------
显示需要写入的内容:
Hello world
文本文件的读取方法
文本文件的写入方法

使用上下文管理关键字with方法

s = 'Hello world\n文本文件的读取方法\n文本文件的写入方法\n'
with open('sample.txt', 'a+') as f:
f.write(s)
with open('sample.txt','r') as src, open('sample_new.txt', 'w') as dst:
dst.write(src.read())
with open('sample_new.txt', 'r') as fp:
for line in fp:
print(line)
第一个文件操作案例。Hello world

文本文件的读取方法

文本文件的写入方法

Hello world

文本文件的读取方法

文本文件的写入方法
fp.closed
True

2.读取文件内容

fr = open('sample.txt', 'r')
print(fr.read(4))
xxx的
print(fr.read(18))
第一个文件操作案例。Hello wo
print(fr.read())
rld
文本文件的读取方法
文本文件的写入方法
Hello world
文本文件的读取方法
文本文件的写入方法
fr.close()
fr.closed
True

3.JSON知识点学习

import json
x = ['yu','bright','1','4','5']
x_bianma = json.dumps(x) # 利用json的dumps对列表x进行字符串编码操作
x_bianma
'["yu", "bright", "1", "4", "5"]'
x_jiema = json.loads(x_bianma)
x_jiema == x # 解码后与x相同类型
True
x_bianma == x # 编码后与x不同类型
False
f_ = open('sample.txt', 'w')
json.dump({'a':1,'b':2,'c':3}, f_) # 对字典进行编码并写入文件
f_.close()

4.读取并显示文件所有内容

with open('sample.txt', 'r') as fp:
while True:
line = fp.readline()
if not line:
break
print(line)
{"a": 1, "b": 2, "c": 3}
with open('sample_new.txt', 'r') as fp:
for line in fp:
print(line)
第一个文件操作案例。Hello world

文本文件的读取方法

文本文件的写入方法

Hello world

文本文件的读取方法

文本文件的写入方法
with open('sample_new.txt','r') as fp:
lines = fp.readlines() # 操作大文件是不建议这样使用
print(''.join(lines))
第一个文件操作案例。Hello world
文本文件的读取方法
文本文件的写入方法
Hello world
文本文件的读取方法
文本文件的写入方法

5.移动文件指针

fp = open('sample_new.txt','r+')
fp.tell() # 返回指针当前位置
0
fp.read(20) # 读取20个字符
'第一个文件操作案例。Hello '
fp.seek(13)  #重新定位文件指针的位置
13
fp.write('测试')
fp.seek(0)
0
fp.read()
'测试文件操作案例。Hello world\n文本文件的读取方法\n文本文件的写入方法\nHello world\n文本文件的读取方法\n文本文件的写入方法\n'
fp.close()

最新文章

  1. RMAN备份失败: ORA-19502 & ORA-27072: File I/O error
  2. redmine常见问题
  3. iOS之 PJSIP蓝牙外设音频支持
  4. Hide SSH Welcome Banner/Message on Ubuntu14.04+
  5. 我为什么要做富文本编辑器【wangEditor5个月总结】
  6. POJ 2559 Largest Rectangle in a Histogram
  7. Java Main Differences between HashMap and HashTable
  8. javascript 一些常用的验证
  9. 重装系统必做之——更换Windows系统的默认临时文件的存储目录
  10. js 字符串日期 转成 Date
  11. Servlet,JDBC,JSONObject三者配和处理客户端请求并返回正确的json数据
  12. RTNETLINK answers: File exists错误
  13. Eclipse启动Tomcat访问不了首页
  14. weblogic启动报错之建域时未指定AdminServer的监听IP的引起的子节点启动故障
  15. 第七届河南省赛A.物资调度(dfs)
  16. Django实战,小网站实现增删改查
  17. springboot全局异常处理
  18. SSM-SpringMVC-25:SpringMVC异常顶级之自定义异常解析器
  19. asp.net core部署时自定义监听端口,提高部署的灵活性
  20. Saddle Point ZOJ - 3955(求每个值得贡献)

热门文章

  1. python2 与 python3 语法区别
  2. UML和模式应用5:细化阶段(8)---逻辑架构和UML包图
  3. Linux中THIS_MODULE宏定义详解
  4. CROSSUI桌面工具 分布加载模块(Distributed UI Module) 与 主模块Module 之间数据传输!
  5. 001_vagrant利器
  6. Servlet注释与部署描述符
  7. transfer pdf to png
  8. PYTHON-模块 sys os random shutil
  9. 2018-2019-2-20175225 实验二《Java开发环境的熟悉》实验报告
  10. mysql的undo log和redo log