文件操作

4.1 文件基本操作

 obj = open('路径',mode='模式',encoding='编码') # 表示要干嘛 读 还是写
obj.write() #写什么内容
obj.read() #读到哪里,全读
obj.close()

4.2 打开模式

- r / w / a
- r+ / w+ / a+
- rb / wb / ab
- r+b / w+b / a+b

4.3 操作

- read() , 全部读到内存

- read(1)

- 1表示一个字符

 obj = open('a.txt',mode='r',encoding='utf-8')
data = obj.read(1) # 1个字符
obj.close()
print(data)
 obj = open('a.txt',mode='rb')
data = obj.read(3) # 1个字节
obj.close()

- write(字符串)

 obj = open('a.txt',mode='w',encoding='utf-8')
obj.write('中午你')
obj.close()

- write(二进制)

 obj = open('a.txt',mode='wb')

 # obj.write('中午你'.encode('utf-8'))
v = '中午你'.encode('utf-8')
obj.write(v) obj.close()

- seek(光标字节位置),无论模式是否带b,都是按照字节进行处理。

 obj = open('a.txt',mode='r',encoding='utf-8')
obj.seek(3) # 跳转到指定字节位置
data = obj.read()
obj.close() print(data) obj = open('a.txt',mode='rb')
obj.seek(3) # 跳转到指定字节位置
data = obj.read()
obj.close() print(data)

- tell(), 获取光标当前所在的字节位置

 obj = open('a.txt',mode='rb')
# obj.seek(3) # 跳转到指定字节位置
obj.read()
data = obj.tell() #获取光标当前所在的字节位置
print(data)
obj.close()

- flush,强制将内存中的数据写入到硬盘

 v = open('a.txt',mode='a',encoding='utf-8')
while True:
val = input('请输入:')
v.write(val)
v.flush() #这里flush已经把内容强行写进硬盘里了 v.close() #关闭文件、保存文件 2个功能

4.4 关闭文件

文艺青年

 v = open('a.txt',mode='a',encoding='utf-8')

 v.close()

二逼

 with open('a.txt',mode='a',encoding='utf-8') as v:
data = v.read() # 错误 mode='a' 只能写,不能读
data=v.write()
# 缩进中的代码执行完毕后,自动关闭文件

4.5 文件内容的修改

 with open('a.txt',mode='r',encoding='utf-8') as f1:
data = f1.read()
new_data = data.replace('飞洒','') with open('a.txt',mode='w',encoding='utf-8') as f1:
data = f1.write(new_data)

**大文件修改(一行一行读出来操作)**【创建】

 f1 = open('a.txt',mode='r',encoding='utf-8')
f2 = open('b.txt',mode='w',encoding='utf-8')
for line in f1: #content= f1.readlines() 得到的是个列表,列表的元素是文件中每一行内容+换行符(字符串)
new_line = line.replace('阿斯','死啊')
f2.write(new_line)
f1.close()
f2.close()
 with open('a.txt',mode='r',encoding='utf-8') as f1, open('c.txt',mode='w',encoding='utf-8') as f2:
for line in f1:
new_line = line.replace('阿斯', '死啊')
f2.write(new_line)

Python基础————文件操作的更多相关文章

  1. python基础-文件操作

    一.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ,只读模式[默认模式,文件必须存在,不存在则抛出异 ...

  2. python基础-文件操作(10)

    一.什么是文件 等等这些都叫做文件,各种格式的.但不仅仅限制于这些. 二.文件的作用 大家应该听说过一句话:“好记性不如烂笔头”. 不仅人的大脑会遗忘事情,计算机也会如此,比如一个程序在运行过程中用了 ...

  3. Python基础--文件操作和集合

    这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 现有以下文件file.txt: 我们哭了 ...

  4. python 基础文件操作

    实时刷新到硬盘里 f= open('hh','w',encoding='utf8') f.write('gyftyftft') f.write('hghgh\njkkjk') f.flush()#实时 ...

  5. Python 基础 文件操作

    字符串与字节之间的转换 # utf-8 一个汉字 占三个字节 # gbk 一个汉字 占两个字节 # 字符串转换成字节 print(bytes('汉字', encoding='utf-8'))print ...

  6. python基础--文件操作实现全文或单行替换

    python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开. 替换文本中的taste 为 tasting Yesterday whe ...

  7. Python基础—文件操作(Day8)

    一.文件操作参数 1.文件路径 1)绝对路径:从根目录开始一级一级查找直到找到文件. f=open('e:\文件操作笔记.txt',encoding='utf-8',mode='r') content ...

  8. python基础 — 文件操作

    读取键盘输入 Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.如下: raw_input input raw_input函数 raw_input([prompt]) 函数从 ...

  9. Python基础-文件操作(七)

    一.文件基本操作 1.open 打开模式: w模式 写模式write 文件不存在时会创建文件,如果文件已存在则会清空文件 r模式 读模式read 文件不存在就报错,存在则准备读取文件 a模式 追加模式 ...

随机推荐

  1. 【hdu 1112】The Proper Key

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  2. 2019-11-17-dotnet-core-使用-GBK-编码

    title author date CreateTime categories dotnet core 使用 GBK 编码 lindexi 2019-11-17 16:36:27 +0800 2019 ...

  3. H3C创建本地用户

    [H3C]Local-user wang                 //创建本地用户--对应上面scheme的 [H3C-luser-wang]Password cipher 456      ...

  4. Visual Studio插件【一】:前端

    JQuery Code Snippets https://github.com/kspearrin/Visual-Studio-jQuery-Code-Snippets 简单用法 jq   +tab ...

  5. KEIL5.11安装小结

    一.注意点 1.安装路径不能带中文,必须是英文路径 2.安装目录不能跟 51 的 KEIL 或者 KEIL4 冲突,三者目录必须分开 3.KEIL5 不像 KEIL4 那样自带了很多厂商的 MCU 型 ...

  6. 谈谈模型融合之一 —— 集成学习与 AdaBoost

    前言 前面的文章中介绍了决策树以及其它一些算法,但是,会发现,有时候使用使用这些算法并不能达到特别好的效果.于是乎就有了集成学习(Ensemble Learning),通过构建多个学习器一起结合来完成 ...

  7. 适配器模式 iOS

    前言: 最近需求作一个公共空间的需求,最后决定用适配器模式来做. 首先,需求是什么? 在我们app中,会有很多列表,tableviewcell的样式会比较统一(当然,我之前在公司那个app不算很大,基 ...

  8. 快速部署 Spring PetClinic 到函数计算平台

    简介 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算准 ...

  9. 网络状态诊断工具——netstat命令

    netstat命令可以用来查询整个系统的网络状态.百度百科的定义如下: Netstat的定义是: Netstat是在内核中访问网络连接状态及其相关信息的程序,它能提供TCP连接,TCP和UDP监听,进 ...

  10. DataX-MysqlWriter 插件文档

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...