python学习之文件管理

在python中文件的管理分为三大步骤:
  • 打开文件: f = open(”filename”)
  • 处理文件:读f.read();f.write(“hello”)
  • 关闭并保存文件:f.close()
打开文件的几种模式总结:

1.”r” 模式:
- 若文件不存在,直接报错;
- 文件只能读取,不能写入;

In [12]: f = open("westos.txt","r")   #打开不存在的文件时报错
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-12-9e69487b8e77> in <module>()
----> 1 f = open("westos.txt","r")

IOError: [Errno 2] No such file or directory: 'westos.txt'

In [13]: f = open("test.txt","r")  #打开一个已经存在的文件

In [14]: f.read()  #查看文件内容
Out[14]: '123\n'

In [15]: f.wr
f.write       f.writelines  

In [15]: f.write("hk")   #向文件中写入时报错
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-15-bc19f3868288> in <module>()
----> 1 f.write("hk")

IOError: File not open for writing

2.”w”模式:
- 若文件不存在,直接创建文件;
- 文件只能写入,不能读取;
- 打开文件时会清空原有文件内容;

3.”r+”模式:
- 若文件不存在,直接报错;
- 文件可以读也可以写;
- 具体写入的内容在哪里,由文件指针(句柄)决定;

4.”w+”模式:
- 若文件不存在,直接创建文件;
- 文件既能写入也能读取;
- 打开文件时会清空原有文件内容;

5.”a”模式:
- 若文件不存在,直接创建文件;
- 文件只能写入,不能读取;
- 写入的内容直接追加到文件最后;

6.”a+”模式:
- 若文件不存在,直接创建文件;
- 文件既能写入也能读取;
- 写入的内容直接追加到文件最后;

7.”b”模式:
- 以二进制的方式打开文件,可以跟r,w,+等组合使用;

文件的读取:

read() # 直接读取文件所有内容;

In [19]: f = open("test.txt")

In [20]: f.read()
Out[20]: 'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:/sbin/nologin\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\nadm:x:3:4:adm:/var/adm:/sbin/nologin\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n#halt:x:7:0:halt:/sbin:/sbin/halt\n'

readline() # 每次读取文件一行内容,返回类型为字符串;

In [30]: f.readline()
Out[30]: 'root:x:0:0:root:/root:/bin/bash\n'

In [31]: f.readline()
Out[31]: 'bin:x:1:1:bin:/bin:/sbin/nologin\n'

In [32]: f.readline()
Out[32]: 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n'

readlines() # 读取文件所有内容,以列表形式返回;

In [41]: f.readlines()
Out[41]: 
['root:x:0:0:root:/root:/bin/bash\n',
 'bin:x:1:1:bin:/bin:/sbin/nologin\n',
 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n',
 'adm:x:3:4:adm:/var/adm:/sbin/nologin\n',
 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n',
 'sync:x:5:0:sync:/sbin:/bin/sync\n',
 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n',
 '#halt:x:7:0:halt:/sbin:/sbin/halt\n']

xreadlines() # 类似生成器,文件内容不直接以列表方式返回,可通过for循环查看:

In [43]: f.xreadlines()
Out[43]: <open file 'test.txt', mode 'r' at 0x1c8ac00>

In [44]: a = f.xreadlines()

In [45]: for i in a:
   ....:     print i,
   ....:     
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
文件的写入:

write(“str”) # 将字符串写入文件

In [60]: f = open("test.txt","a+")  #以a+模式打开文件

In [61]: f.write("hello")   #写入

In [62]: f.write("hello","haha")  # 写入多个时报错
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-8993e1a4dee9> in <module>()
----> 1 f.write("hello","haha")

writelines(“[“hello\n”,”world\n”]”) # 将序列的每个元素写入文件;

In [81]: f.writelines(["westos\n","haha\n"]) # 写入2个元素
In [82]: f.readlines()
Out[82]: 
['root:x:0:0:root:/root:/bin/bash\n',
 'bin:x:1:1:bin:/bin:/sbin/nologin\n',
 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n',
 'adm:x:3:4:adm:/var/adm:/sbin/nologin\n',
 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n',
 'sync:x:5:0:sync:/sbin:/bin/sync\n',
 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n',
 '#halt:x:7:0:halt:/sbin:/sbin/halt\n',
 'hellowestos\n',
 'haha\n']
文件的其它操作:

f.tell() #查看当前指针所处位置
f.seek(偏移量,选项)
偏移量如果为正数,代表向右偏移;如果为负数,代表向左偏移;

选项如果为0,指针指向文件开始;
如果为1,指针指向当前位置;
如果为2,指针指向文件末尾;
f.flush() # 提交对文件的修改;

需要注意的点:

-文件对象是可迭代数据类型,可以通过for遍历文件的内容;
- with open(“filename”) as f:通过这种方法来避免用户忘记关闭文件的问题;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值