Python之路,Day21- Python基础-文件操作方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sj349781478/article/details/81670949

一、文件操作方法


f = open('file1','r+',encoding='utf-8')

f.read() #读取所有内容,光标移动到文件末尾

f.readline() #读取一行内容,光标移动到第二行首部

f.readlines() #读取每一行内容,存放于列表中

f.write('1111\n222\n'#针对文本模式的写,需要自己写换行符

f.write('1111\n222\n'.encode('utf-8')) #针对b模式的写,需要自己写换行符

f.writelines(['333\n','444\n']) #文件模式

f.writelines([bytes('333\n',encoding='utf-8'),'444\n'.encode('utf-8')]) #b模式

#了解

f.readable() #文件是否可读

f.writable() #文件是否可读

f.closed #文件是否关闭

f.encoding #如果文件打开模式为b,则没有该属性

f.flush() #立刻将文件内容从内存刷到硬盘

f.name

f.read()

  • 读取所有内容,光标移动到文件末尾。

***Python.txt内容***

apple 100000 1

mac 3000 2    

lenovo 30000 3

chicken 10 3

f =  open('file1','r',encoding='utf8'

print(f.read())

'''内容输出'''   

 apple 100000 1

 mac 3000 2    

 lenovo 30000 3

 chicken 10 3

f.readline()

  • 读取一行内容,光标移动到第二行首部。

实例1(Python3.0+):

***Python.txt内容***

apple 100000 1

mac 3000 2    

lenovo 30000 3

chicken 10 3

f =  open('file1','r',encoding='utf8'

print(f.readline()) # --> 执行'f.readline()'后,光标移动到第二行首位

 apple 100000 1

    print(f.read()) # --> 此时读取内容是,光标在第二行首位。

 mac 3000 2

 lenovo 30000 3

 chicken 10 3

实例2(Python3.0+):

***Python.txt内容***

床前明月光,

疑是地上霜。

举头望明月,

低头思故乡。

f =  open('file1','r+',encoding='utf8')

print(f.readline().strip())

    # 床前明月光,

f.write('Python')  # 写到文件最后

print(f.readline())

    # 疑是地上霜。

f.seek(0)   # 移动光标到第一行行首

print(f.read())

    # 床前明月光,

    # 疑是地上霜。

    # 举头望明月,

    # 低头思故乡。Python

小结:

1、使用r+模式打开文件,f.readline()每次读取一行内容,光标移动到下一行行首

2、执行f.readline()后再执行写入f.write()时,内容写到文件最后

3、再次执行f.readline()后,文件仍然读取第二行内容。

f.readlines()

  • 逐一读取每一行内容,存放于列表中。

***Python.txt内容***

apple 100000 1

mac 3000 2    

lenovo 30000 3

chicken 10 3

f =  open('file1','r',encoding='utf8')

for in f.readlines():

   print(i.strip())

   print('***')

'''内容输出'''

apple 100000 1

***

mac 3000 2

***

lenovo 30000 3

***

chicken 10 3

***

练习题:文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数

apple 10 3
tesla 100000 1
mac 3000 2
lenovo 30000 3
chicken 10 3

f =  open('file1','r',encoding='utf8')

sum =  

for in f.readlines():

    # 把读取到的每行内容变成列表

    = i.strip().split(' ')

    # print(i)

    sum = sum + int(i[1])*int(i[2])

print(sum)

    # 196060

f.write()

***Python.txt内容***

apple 100000 1

f =  open('file1','w',encoding='utf8'

f.write('1111\n2222\n')

f =  open('file1','r',encoding='utf8')

print(f.read())

'''内容输出'''

 1111

 2222

w或w+模式时,打开文件时会清空文件内容

 

其它操作


f.tell()

  • 打印光标位置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

***Python.txt内容***

apple 100000 1

f =  open('file1','r',encoding='utf8')

print(f.tell())

print(f.read(2))

print(f.tell())

***内容输出***

 0

 ap

 2

# 打印光标位置,文件内容为英文时,每个英文代表一个位置,为中文时,每个中文代表3个位置(因为读取时,使用的utf - 8编码,如果使用gbk编码,则占用2个位置)

f.seek()

  • 调整光标位置

***Python.txt内容***

床前明月光,

疑是地上霜。

举头望明月,

低头思故乡。

f =  open('file1','r',encoding='utf8'

print(f.read(2))    # 床前

print(f.tell())  # 显示此时光标位置在6,一个中文代表3个位置

print(f.read(2))  # 打印输出为:明月

f.seek(0)  # 光标回到首位

print(f.read(2))  # 打印输出为:床前

f.flush()

  • 把缓存中的内容存放到磁盘中

***Python.txt内容***

床前明月光,

疑是地上霜。

举头望明月,

低头思故乡。

= open('file1','w')  # 使用w模式打开文件时,清空文件内容

k.write('sj is 35'# 此时'alex is 35'存放在缓存中

k.write('\nhello world'# 此时'hello world'存放在缓存中

k.flush() # 把缓存中的内容存放到磁盘中,也就是存放到file1中

'''执行代码后文件内容'''

 sj is 35

 hello world

f.truncate()

  • 内容截断
  • 默认从光标为0的位置删除内容
 

***Python.txt内容***

床前明月光,

疑是地上霜。

举头望明月,

低头思故乡。

= open('file1','r+')

f.truncate()  # 默认从光标为0的位置删除内容,删除0之后的所有内容。

猜你喜欢

转载自blog.csdn.net/sj349781478/article/details/81670949