python文件操作

python文件操作

一、文件操作

简介:

  我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知,应用程序是无法直接操作硬件的,这就用到了操作系统。操作系统把复杂的硬件操作封装成简单的接口给用户/应用程序使用,其中文件就是操作系统提供给应用程序来操作硬盘虚拟概念,用户或应用程序通过操作文件,可以将自己的数据永久保存下来。

二、文件的打开方式

1 文件句柄方式

# 1.打开文件,得到文件句柄并赋值给一个变量
f = open('file.txt',mode='r',encoding='utf-8')
# 2.通过句柄对文件进行操作
content = f.read()
# 3.输入读取到的结果
print(content)
# 4.关闭文件
f.close()

 

2 上下文的方式

# 1 with 关键字引导上下文方式打开文件,并将句柄赋于别名f
with open('file.txt',mode='r',encoding='utf-8') as f:
    #2  通过句柄对文件进行操作
    content = f.read()
    #3 输出读取到的结果,用with上下文方式对文件进行操作会在借宿操作时默认关闭文件,不必要主动close() 
    print(content)

 

三、操作文件  

1.读  

# 此处demo文件中原始内容为:我在复习,文件操作
#1 r --只读,文件不存在会报错
with open('demo',mode='r',encoding='utf-8') as f:
    content = f.read()
    print(content)
>>>我在复习,文件操作


#2 rb--以bytes类型读
with open('demo',mode='rb') as f:
    content = f.read()
    print(content)
>>>b'\xe6\x88\x91\xe5\x9c\xa8\xe5\xa4\x8d\xe4\xb9\xa0\xef\xbc\x8c\xe6\x96\x87\xe4\xbb\xb6\xe6\x93\x8d\xe4\xbd\x9c'

#3 r+ --读写,文件不存在不会创建,写则会在指针后添加内容
with open('demo',mode='r+',encoding='utf-8') as f:
    content = f.read()
    f.write('\n新添加:文件操作的r+功能')
    print(content)
>>>我在复习,文件操作
在demo文件中
'''
我在复习,文件操作
新添加:文件操作的r+功能
'''


#4.r+b--以bytes类型读写
with open('demo',mode='r+b') as f:
    content = f.read()
    f.write('\n新添加:文件操作的r+功能'.encode('utf-8'))  ##必须要转码,不然会报错
    print(content)
>>>b'\xe6\x88\x91\xe5\x9c\xa8\xe5\xa4\x8d\xe4\xb9\xa0\xef\xbc\x8c\xe6\x96\x87\xe4\xbb\xb6\xe6\x93\x8d\xe4\xbd\x9c\r\n\xe6\x96\xb0\xe6\xb7\xbb\xe5\x8a\xa0\xef\xbc\x9a\xe6\x96\x87\xe4\xbb\xb6\xe6\x93\x8d\xe4\xbd\x9c\xe7\x9a\x84r+\xe5\x8a\x9f\xe8\x83\xbd\r\n\xe6\x96\xb0\xe6\xb7\xbb\xe5\x8a\xa0\xef\xbc\x9a\xe6\x96\x87\xe4\xbb\xb6\xe6\x93\x8d\xe4\xbd\x9c\xe7\x9a\x84r+\xe5\x8a\x9f\xe8\x83\xbd\r\n\xe6\x96\xb0\xe6\xb7\xbb\xe5\x8a\xa0\xef\xbc\x9a\xe6\x96\x87\xe4\xbb\xb6\xe6\x93\x8d\xe4\xbd\x9c\xe7\x9a\x84r+\xe5\x8a\x9f\xe8\x83\xbd'

2.写

此处demo文件中原始内容为空,需要向其中写入:我在复习,文件操作

#1 w -- 只写,文件不存在则创建,文件存在则清空在写
with open('demo',mode='w',encoding='utf-8') as f:
    content = f.write('我在复习,文件操作')
>>>在demo文件中的内容为
    我在复习,文件操作


#2.x--只写,文件不存在则创建,文件存在则报错
with open('demo',mode='x',encoding='utf-8')as f:
    content = f.write('我在复习,文件操作')
因为demo中存在‘我在复习,文件操作’
Traceback (most recent call last):
  File "D:/untitled/假期/2018-2-24/文件操作.py", line 64, in <module>
    with open('demo',mode='x',encoding='utf-8')as f:
FileExistsError: [Errno 17] File exists: 'demo'

因为demo中不存在
>>>我在复习,文件操作


#3 wb -- 以bytes类型写
with open('demo',mode='wb')as f:
    content = f.write('我在复习,文件操作'.encode('utf-8'))
>>>我在复习,文件操作


#4.w+ -- 写读,文件不存在则创建,写会覆盖之前的内容
with open('demo', mode='w+') as f:
    content = f.write('hello,world')
    date = f.read()
    print(date)
>>># demo中的内容为:
>>> hello,world        之前的内容被覆盖


#5 w+b --以bytes类型写读
with open('demo', mode='w+b') as f:
    content = f.write('hello,world'.encode('utf-8'))
    date = f.read()
    print(date)

>>>b''
>>>demo中的内容为:
>>> hello,world  

3.追加

此处demo文件中原始内容为hello,world
#1.a -- 追加,文件不存在则创建,文件存在则追加
with open('demo',mode='a',encoding='utf-8') as f:
    content = f.write('哈哈')
demo 中的内容为
>>>hello,world哈哈


#2 ab -- 以bytes类型追加
with open('demo',mode='ab')as f:
    content = f.write('啊哈哈'.encode('utf-8'))
>>>hello,world啊哈哈


#3 a+ --可读可写,文件不存在则创建,写则追加
with open('demo', mode='a+',encoding='utf-8') as f:
    content = f.write('谁用谁知道')
    f.seek(0)
    date = f.read()
    print(date)
>>>hello,world谁用谁知道


#4.a+b——以bytes类型可读可写
with open('demo', mode='a+b') as f:
    content = f.write('谁用谁知道'.encode('utf-8'))
    f.seek(0)
    date = f.read()
    print(date)
b'hello,world\xe5\x95\x8a\xe5\x93\x88\xe5\x93\x88\xe8\xb0\x81\xe7\x94\xa8\xe8\xb0\x81\xe7\x9f\xa5\xe9\x81\x93\xe8\xb0\x81\xe7\x94\xa8\xe8\xb0\x81\xe7\x9f\xa5\xe9\x81\x93'
demo文件中   hello,world谁用谁知道

4.文件修改

python没有提供直接修改文件的函数,只能新建文件,将原文件的内容修改完成后写入新文件中,再把原文件删除,新文件重命名

 

import os

with open('源文件', encoding='utf-8') as f, open('源文件.bak', mode='w', encoding='utf-8') as f_w:
    for i in f:
        if '源文件' in i:
            i = i.replace('源文件', '更改后的源文件')
        f_w.write(i)  # 写文件

os.remove('源文件')  # 删除文件
os.rename('源文件.bak', '源文件')  # 重命名文件

 

  

5、其他操作

1.seek()移动光标指针位置

seek有三种移动方式0,1,2,其中1和2必须在b模式下进行,但无论哪种模式,都是以bytes为单位移动的

2.tell()返回当前指针所在的位置

tell对于英文字符就是占一个,中文字符占三个,参数表示的是字节数区分与read()的不同.

3.truncate() 截断文件

truncate是截断文件,所以文件的打开方式必须可写,但是不能用w或w+等方式打开,因为那样直接清空文件了,所以truncate要在r+或a或a+等模式下测试效果

4.readline() 读取一行

5.readlines() 读取多行,返回为列表

6.readable() 文件是否可读

7. writeline() 写入一行

8. writelines() 写入多行

9.writable() 文件是否可读

10.closed() 文件是否关闭

11.encoding=’utf-8’ 如果文件打开模式为b,则没有该属性

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

13.for循环文件句柄

 

posted @ 2018-02-24 10:10  流年中渲染了微笑  阅读(686)  评论(0编辑  收藏  举报