对文件操作流程

  1. 打开文件,得到文件句柄并赋值给一个变量
  2. 通过句柄对文件进行操作
  3. 关闭文件

现有文件如下 命名为7 years

Once I was seven years old my momma told me
那年我七岁 妈妈就对我说
Go make yourself some friends or you'll be lonely
去交些朋友 不然你会孤独寂寞
Once I was seven years old
那年我七岁
It was a big big world but we thought we were bigger
这个辽阔的大千世界 总以为我们也会变得更加强大
Pushing each other to the limits we were learning quicker
将彼此逼到绝境 我们得以更快的成长
By eleven smoking herb and drinking burning liquor
十一岁那年 我吸大麻 喝烈性酒
Never rich so we were out to make that steady figure
生活捉襟见肘 我们离家奋斗只为有个稳定收入
Once I was eleven years old my daddy told me
十一岁那年 爸爸对我说
Go get yourself a wife or you'll be lonely
给自己找个妻子 否则你会空虚寂寞
Once I was eleven years old
那年我十一岁
I always had that dream like my daddy before me
我常梦想着有朝一日能像爸爸一样成为一个歌手
So I started writing songs I started writing stories
所以我开始写歌 开始写不同的故事
Something about the glory just always seemed to bore me
曾经的光辉岁月 对我来说也已厌倦
Cause only those I really love will ever really know me
因为只有那些我真正爱的人才真的懂我
Once I was 20 years old my story got told
二十岁那年 我的故事广为流传
Before the morning sun when life was lonely
黎明还未照耀前 孤独的我无人相伴
Once I was 20 years old
那年我二十岁

 以下代码是一些基本操作,可以把相应的注释符去掉,逐一验证

 #-*-coding:utf-8-*-
#_author_:Keep #data = open('7 years',encoding='utf-8').read()
'''
#只读模式
f = open('7 years','r',encoding='utf-8')#文件句柄
data = f.read()
print(data)
'''
'''
#只写模式
f = open('随笔','w',encoding='utf-8')#w,只写模式,不可读;不存在则创建;存在则删除内容;
f.write('你的孤独,\n')
f.write('虽败犹荣。')
'''
'''
#追加模式 a = append 追加
f = open('7 years','a',encoding='utf-8')
f.write('\n你的孤独,\n')
f.write('虽败犹荣。')
f.close()
'''
'''
#读前两行
f = open('7 years','r',encoding='utf-8')
for i in range(2):
print(f.readline())
'''
'''
f = open('7 years','r',encoding='utf-8')
#print(f.readlines())#把文件转化为列表
for line in f.readlines():
print(line.strip())#strip去掉换行和空格
'''
'''
#(较low)在第五行后取分割线
f = open('7 years','r',encoding='utf-8')
for index,line in enumerate(f.readlines()):
if index == 5:
print('---------分割线-----------')
continue
print(line.strip())
'''
#(高逼格)在第五行后取分割线
f = open('7 years','r',encoding='utf-8')
count = 0
for line in f:
if count == 5:
print('------------分割线-----------')
print(line)
count += 1

tell和seek的配合

f = open('7 years','r',encoding='utf-8')
print(f.tell())#打印光标所在位置
print(f.readline())
print(f.readline())
print(f.tell())
f.seek(10)#把光标回到指定位置
print(f.readline())
print(f.encoding)
print(f.name)#打印文件名
print(f.readable())#判断文件是否可读
print(f.writable())#判断文件是否可写
print(f.closed)#判断文件是否关闭

其他有趣的文件操作

flush的作用就是每写一行就刷进硬盘,以下是演示过程

#好玩的进度条
import sys,time#导入sys和time模块
for i in range(10):
sys.stdout.write("%")
sys.stdout.flush()
time.sleep(1)#时间间隔1s
截断truncate的用法:
f = open('7 years','a',encoding='utf-8')
#f.truncate()#括号什么都不写表示清空文件里的内容
f.truncate(10)#指定截断
r+ , a+ , w+, rb, wb的用法:
#f = open('7 years','r+',encoding='utf-8')#读写
#f = open('7 years','w+',encoding='utf-8')#写读
#f = open('7 years','a+',encoding='utf-8')#追加读写
#f =open("7 years",'rb')#读二进制文件 以下两种情况需要用到此方法:1,网络传输 2,下载
f = open("7 years","wb")#写二进制文件
f.write("hello!".encode())
f.close()
'''
print(f.tell())
f.write("----------7 years-----------")
'''

 文件的修改,以下代码对7 years文件的英文和中文同时修改。

f = open("7 years",'r',encoding='utf-8')
f_new = open("7 years.new",'w',encoding='utf-8') for line in f:
if "那年我七岁" in line:
line = line.replace("那年我七岁","那年我八岁")
if "Once I was seven years old" in line:
line = line.replace("Once I was seven years old","Once I was eight years old")
f_new.write(line)
f.close()
f_new.close()  

 with的用法:

#with作用自动关闭文件
with open("7 years","r",encoding="utf-8") as f:
for line in f:
print(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基础————文件操作

    文件操作 4.1 文件基本操作 obj = open('路径',mode='模式',encoding='编码') # 表示要干嘛 读 还是写 obj.write() #写什么内容 obj.read() ...

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

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

  9. python基础 — 文件操作

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

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

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

随机推荐

  1. 小妖精的完美游戏教室——buff系统

    作者:小妖精Balous,未经作者允许,任何个人与单位不得将此源代码用于商业化项目 #region buff /// <summary> /// 是否魔法免疫,魔法免疫的生物不会受到除自己 ...

  2. highchart在IE8下面的显示问题解决

    完整的代码: <!DOCTYPE HTML><html> <head> <meta http-equiv="Content-Type" c ...

  3. day1||python

    测试题: 0. Python 是什么类型的语言? Python是一种面向对象.解释型.动态类型计算机程序设计语言解释型:程序无需编译成二进制代码,而是在执行时对语句一条一条编译动态类型:在程序执行过程 ...

  4. [Python学习笔记] turtle库的基本使用

    turtle库常用函数 引入turtle模块 import turtle turtle的绘图窗体 #setup()设置窗口大小及位置#setup()可省略turtle.setup(width,heig ...

  5. Nginx配置之负载均衡、限流、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...

  6. java基础-arrayList

      ArrayList: 结构之钱了解了,ArrayList()会构造出一个初始容量=10的空的列表: ArrayList()的增加和删除都是拷贝数组到新的数组(如果当前数组容量不足的话),把数组内的 ...

  7. c标签 多个条件

    <c:if test="${(rwyy01.yyry==NULL || rwyy01.yyry=='') && (rwyy01.shry==NULL || rwyy01 ...

  8. redux源码解读(二)

    之前,已经写过一篇redux源码解读(一),主要分析了 redux 的核心思想,并用100多行代码实现一个简单的 redux .但是,那个实现还不具备合并 reducer 和添加 middleware ...

  9. 第一章 XAML概览

    1.1 XAML是什么? XAML是eXtensible Application Markup Language的英文缩写,相应的中文名称为可扩展应用程序标记语言.也就是说在开发一个应用程序时,我们可 ...

  10. 【网络】IP子网划分详解

    1.IP地址组成                                IP地址组成示意图 IP地址由32位二进制组成,32位二进制分成了4字节,每字节8位,字节之间用符.(点)分隔,为了方便 ...