Python菜鸟编程第五课之文件操作

Python菜鸟编程第五课之文件操作

1文件操作

一个换行符占一个字节。

1.1文件的打开–open()

格式:

file_object=open(file,[,mode],[,encoding],[,buffering])

解释:

  • file_object:open()返回的文件对象
  • file:要打开的文件
  • mode:指定模式(只读,写入,追加等等。默认只读)
  • encoding:指定文件编码,通常为:‘utf-8/gbk’
  • buffering:若设置为0,不会有寄存;设为1,访问文件时会寄存行;设置为大于1的数,表明是寄存区的缓冲大小;设置为负数时,则缓存区的大小为系统默认。
mode描述
r只读,指针在文本最前(0,0),只能进行读,不可写,这是默认模式
rb以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+打开一个文件用于读写。文件指针将会放在文件的开头。
rb+以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w写,指针在文本最前(0,0),如果没有目标文本,会新建一个空目标文本;如果有,会清除目标文本中的所有内容,相当于新建一个空的目标文本后替换原文本
wb以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a追加,如果有目标文本,指针会跳到文本的最后;如果没有找到,同w模式会新建一个空的文本。``
ab以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

文件的属性

属性描述
name返回文件的名称
mode返回打开文件访问模式
closed如果文件已关闭,返回True,否则false

demo:

f = open('file/GreenFlowerPorcelain.txt', 'r')
print(f.read())
print(f.name)
print(f.mode)
print(f.closed)
f.close()
print(f.closed)
运行结果:
 1 Green Flower Porcelain
 2 You are the face that has changed my whole world.
 3 You are the face that I see everywhere I go.
 4 You are so beautiful to me that I can't explain ,
 5 Just like a green flower porcelain.
 6 You're like a moon that I awaken to say hello,
 7 So beautiful and bright that you make me content to play it so.
 8 I see your face on the leaves,telling me how lonely I have been.
 9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
file/GreenFlowerPorcelain.txt
r
False
True

1.2文件的读写–>read()/write()

函数名描述
f.read(size)从文件中读取指定的字节数,若未指定,读取所有。size输入字节数
f.readline()读取整行,包括"\n"
f.readlines()读取所有行,并返回列表
f.write()将字符串写入文件,并返回字符串长度。
f.writelines()向文件写入一个序列字符串列表。如果需要换行,需要自己假如换行符
f.flush()刷新文件缓存,直接把内部缓冲的数据写入文件
1.21文件的读

demo1:

#read()
f = open('file/GreenFlowerPorcelain.txt', 'r')
print(f.read(20))
print(f.read(20))
f.close()
运行结果:
 1 Green Flower Porc#包括空格,总共20个字节,指针停留在当前位置
    elain        #从上面结果的指定位置开始输出,这里换行是因为文件本身有个换行符
 2 You are the

demo2:

#readline()
f = open('file/GreenFlowerPorcelain.txt', 'r')
f1=f.readline()
f2=f.readline()
f3=f.readline()
print(f1)
print(f2)
print(f3)
f.close()
运行结果:
 1 Green Flower Porcelain

 2 You are the face that has changed my whole world.

 3 You are the face that I see everywhere I go.

demo3:

#readlines()
f = open('file/GreenFlowerPorcelain.txt', 'r')
fl=f.readlines()
print(fl)
print(len(fl))
f.close()
运行结果:
运行结果:
[' 1 Green Flower Porcelain\n', ' 2 You are the face that has changed my whole world.\n', ' 3 You are the face that I see everywhere I go.\n', " 4 You are so beautiful to me that I can't explain ,\n", ' 5 Just like a green flower porcelain.\n', " 6 You're like a moon that I awaken to say hello,\n", ' 7 So beautiful and bright that you make me content to play it so.\n', ' 8 I see your face on the leaves,telling me how lonely I have been.\n', ' 9 This is a dream of mine that I have just dreamed.\n', '10 Just see your smiling face everywhere I go.\n', '11 The love I feel for you to shine inside me.\n', "12 But it's all over now you're gone."]
12

demo4:

f = open('file/GreenFlowerPorcelain.txt', 'r')
print(f.tell())
for i in f:
    print(i.strip())  # 移除字符串前后的字符,默认为空格
print(f.tell())
f.close()
运行结果:#使用for循环遍历文件后,指针位置也会跟随移动到文件末尾。txt文件中有换行符,这里使用strip()函数将其移除。
0#指针的初始位置
1 Green Flower Porcelain
2 You are the face that has changed my whole world.
3 You are the face that I see everywhere I go.
4 You are so beautiful to me that I can't explain ,
5 Just like a green flower porcelain.
6 You're like a moon that I awaken to say hello,
7 So beautiful and bright that you make me content to play it so.
8 I see your face on the leaves,telling me how lonely I have been.
9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
599#指针的最后位置
1.22文件的写

demo1:

f = open('file/青花瓷.txt', 'a+',encoding='utf8')
f.seek(0)#使指针回到文件的开头,因为'a+'默认将指针移到文件末尾
print(f.read())
f.write('aaa')
f.seek(0)
print(f.read())
f.close()
运行结果:
 1 青花瓷
 2 素胚勾勒出青花笔锋浓转淡
 3 瓶身描绘的牡丹一如你初妆
 4 冉冉檀香透过窗心事我了然
 5 宣纸上走笔至此搁一半
 6 釉色渲染仕女图韵味被私藏
 7 而你嫣然的一笑如含苞待放
 8 你的美一缕飘散
 9 去到我去不了的地方
10 天青色等烟雨
11 而我在等你
12 炊烟袅袅升起
 1 青花瓷
 2 素胚勾勒出青花笔锋浓转淡
 3 瓶身描绘的牡丹一如你初妆
 4 冉冉檀香透过窗心事我了然
 5 宣纸上走笔至此搁一半
 6 釉色渲染仕女图韵味被私藏
 7 而你嫣然的一笑如含苞待放
 8 你的美一缕飘散
 9 去到我去不了的地方
10 天青色等烟雨
11 而我在等你
12 炊烟袅袅升起aaa

demo2:

f = open('file/GreenFlowerPorcelain.txt', 'rb')
print(f.read(10))
print('wz',f.tell())
f.seek(-5,1)
print('wz',f.tell())
print(f.read(50))
print('wz',f.tell())
f.close()
运行结果:
b' 1 Green F'
wz 10
wz 5 #从当前位置,往前偏移5个字符所以指针处于5
b'een Flower Porcelain\r\n 2 You are the face that has'
wz 55

1.3文件的关闭–>close()

文件打开后都需要关闭,以免浪费内存空间。

还有一个方法,with as语法,会自动关闭文件

demo:

with open('file/GreenFlowerPorcelain.txt', 'r') as f:
    print(f.read())
print(f.closed)
运行结果:
 1 Green Flower Porcelain
 2 You are the face that has changed my whole world.
 3 You are the face that I see everywhere I go.
 4 You are so beautiful to me that I can't explain ,
 5 Just like a green flower porcelain.
 6 You're like a moon that I awaken to say hello,
 7 So beautiful and bright that you make me content to play it so.
 8 I see your face on the leaves,telling me how lonely I have been.
 9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
True

1.4文件的定位

函数名描述
f.seek(offset[,from])设置文件的当前位置,即指针位置,from=0时,文件开头位置,from=1当前位置,from=2文件末尾
f.tell()返回文件的当前位置,即指针位置

博主QQ:1031748759 欢迎批评指正!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值