python文件操作

常见的文件类型

  • txt
  • csv
  • json
  • html
  • mp4

文件操作流程

  • 打开 —>open()
  • 读/写 —>read()/write()
  • 关闭 —>close()
    用途方法功能
    打开文件内容f=open(file)打开文件
    关闭文件f.close()关闭文件
    读写文件内容f.read([size])从文件中读取指定的字节数;若未指定,读取所有
    f.readline([size])读取整行,包括"\n"
    f.readlines([size])读取所有行,并返回列表;
    f.write(str)将字符串写入文件,并返回字符串长度
    f.writelines(seq)项文件写入一个序列字符串。如果需要换行,需要自己加入换行符
    f.flush()刷新文件缓冲,直接把内容缓冲的数据写入文件
    文件的定位f.seek(offset)设置文件的当前位置,即指针位置
    f.tell()返回文件的当前位置,即指针位置

文件方法

模式描述
r以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+打开一个文件用于读写。文件指针将会放在文件的开头。
rb+以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

文件的打开与关闭

打开

open(),创建一个file对象,之后才可以用它调用相关的方法
格式:
file_object=open(file,[,mode],[encoding=“utf-8”],[,buffering])
解释:

  • file_object —>open()返回的文件对象
  • file —>要打开的文件
  • mode —> 指定模式:只读,写入,追加等等。表。一般默认为只读。
    -encoding —>指定文件编码,usually,“utf-8”/“GBK”
  • buffering —>若设置为0,不会有寄存;设为1,访问文件时会寄存行;设为大于1的数,表明是寄存区的缓冲大小;设为负数,则寄存区的缓冲区大小为系统默认
    文件对象的属性
属性描述
f.name返回文件名称
f.mode返回打开文件的访问模式
f.closed如果文件已关闭,返回true;否则返回false
f=open('./data/GreenFlowerPorcelain.txt','r')
f.read()
print(f.name)
'''./data/GreenFlowerPorcelain.txt'''
print(f.mode)
'''r'''
print(f.closed)
'''False'''
f.close()
print(f.closed)
'''True'''

关闭

f.close()
节省计算机资源和提供性能,使用完文件后关闭它

f=open('./data/GreenFlowerPorcelain.txt','r')
f1=f.read(4)
print(f.read(4))
'''reen'''
print(f.read(4))
''' Flo'''
print(len(f1))
'''4'''
f.close()
f = open('./data/GreenFlowerPorcelain.txt', 'r')
f1=f.readline()
f2=f.readline()
f3=f.readline()
print(f1)
print(f2)
print(f3)
f_ls = f.readlines()
print(f_ls)
'''[' 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."]
'''
print(len(f_ls))
'''12'''
f.close()
f1=f.readline(10)
f2=f.readline(10)
print(f1)
''' 1 Green F'''
print(f2)
'''lower Porc'''
f.close()

f = open('./data/GreenFlowerPorcelain.txt', 'r')
for i in f:
    print(i.strip())
'''
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.
'''
f = open('./data/青花瓷.txt', 'r',encoding='utf-8')
print(f.name)
print(f.read(4))  # 读取指定字节数
print(f.readline(7))
print(f.readlines())  # 读取所有行,以列表形式返回
'''
./data/青花瓷.txt
 1 青
花瓷

[' 2 素胚勾勒出青花笔锋浓转淡\n', ' 3 瓶身描绘的牡丹一如你初妆\n', ' 4 冉冉檀香透过窗心事我了然\n', ' 5 宣纸上走笔至此搁一半\n', ' 6 釉色渲染仕女图韵味被私藏\n', ' 7 而你嫣然的一笑如含苞待放\n', ' 8 你的美一缕飘散\n', ' 9 去到我去不了的地方\n', '10 天青色等烟雨\n', '11 而我在等你\n', '12 炊烟袅袅升起\n', '13 隔江千万里\n', '14 在瓶底书汉隶仿前朝的飘逸\n', '15 就当我为遇见你伏笔\n', '16 天青色等烟雨\n', '17 而我在等你\n', '18 月色被打捞起\n', '19 晕开了结局\n', '20 如传世的青花瓷自顾自美丽\n', '21 你眼带笑意\n', '22 色白花青的锦鲤跃然於碗底\n', '23 临摹宋体落款时却惦记着你\n', '24 你隐藏在窑烧里千年的秘密\n', '25 极细腻犹如绣花针落地\n', '26 帘外芭蕉惹骤雨\n', '27 门环惹铜绿\n', '28 而我路过那江南小镇惹了你\n', '29 在泼墨山水画里\n', '30 你从墨色深处被隐去']

'''

  • f.read([size])
  • 从文件指针处读取
  • 传递size,指定读取的字节数
  • readline ()
  • readlines()

write()
writelines()
定位读取
f.tell()

  • 获取文件的当前位置
    f.seek()
  • f.seek()
  • seek()(offet[,from])
  • offet,要移动的字节数
  • from,指定偏移的参考位置
    • 0 —>从头开始偏移
    • 1 —>从当前开始偏移
    • 2 —>相对文件末尾移动
      f=open('./data/GreenFlowerPorcelain.txt','r')
      f1=f.read(10)
      f_s1=f.tell()
      print(f_s1)
      '''
      10
      '''
      
      f.seek(20,0)
      f2=f.read(50)
      print(f2)
      '''
      elain
      '''
      
      f.close()
      with open('./data/GreenFlowerPorcelain.txt','r') as f:
      print(f.read())
      '''
      2 You are the face that has changed my whol
       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.
      '''
      
      print(f.closed)
      '''
      True
      '''
      
      f=open('./data/test.txt','w')
      f.write("zuccs")
      f.flush()
      f=open('./data/test.txt','r')
      print(f.read())
      '''
      zuccs
      '''
      
      f.close()
      import random
      f=open('./data/random.txt','w+')
      for i in range(100):
      f.write(str(random.randint(1,100))+"\t")
      f.seek(0,0)
      print(f.read())
      '''
      60	98	71	33	69	28	41	58	4	4	21	69	81	47	30	54	45	71	84	30	48	55	24	31	81	14	98	56	30	45	57	53	36	91	75	5	48	2	85	17	35	54	13	48	48	13	55	49	33	27	16	94	87	58	18	47	20	42	17	31	83	70	75	87	84	89	4	75	22	29	75	33	20	11	58	3	63	64	20	72	32	21	69	5	25	45	76	7	9	27	4	50	20	31	16	88	19	68	60	14	
      '''
      
       f.close()
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值