Python中的用for,while循环遍历文件实例
2017-12-28 23:32
639 查看
使用for循环遍历文件
打开文件
open
r:以读模式打开
w:以写模式打开
a:以追加模式打开
r+:以读写模式打开
w+:以读写模式打开(参见w)
a+:以读写模式打开(参见a)
rb:以二进制读模式打开
wb:以二进制写模式打开(参见w)
ab:以二进制追加模式打开(参见a)
rb+:以二进制读写模式打开(参见r+)
wb+:以二进制读写模式打开(参见w+)
ab+:以二进制读写模式打开(参见a+)
查看帮助:
open(...)
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
(END)...skipping...
[root@localhost ~]# cat /tmp/1.txt
1111
[root@localhost ~]#
只读方式打开:
In [26]: open('/tmp/1.txt')
Out[26]: <open file '/tmp/1.txt', mode 'r' at 0x20860c0>
In [27]: fd = open('/tmp/1.txt')
In [28]: fd
Out[28]: <open file '/tmp/1.txt', mode 'r' at 0x20861e0>
In [29]: type(fd)
Out[29]: file
以写方式打开:
In [34]: fd = open('/tmp/1.txt','w')
In [35]: fd.write('2222\n')
In [36]: fd.close()
[root@localhost ~]# cat /tmp/1.txt
2222
[root@localhost ~]#
以追加方式打开:
In [34]: fd = open('/tmp/1.txt','a')
In [35]: fd.write('3333\n')
In [36]: fd.close()
[root@localhost ~]# cat /tmp/1.txt
2222
3333
[root@localhost ~]#
read():
In [41]: fd.read()
Out[41]: '2222\n3333\n'
In [42]: fd.read()
Out[42]: ''
In [49]: fd.readline()
Out[49]: '2222\n'
In [50]: fd.readline()
Out[50]: '3333\n'
In [51]: fd.readline()
Out[51]: ''
In [52]:
read() 和readline()返回的是字符串:
readlines()返回的是列表:
in [52]: fd = open('/tmp/1.txt')
In [53]: fd.readlines()
Out[53]: ['2222\n', '3333\n']
脚本:
#!/usr/bin/python
fd = open('/tmp/1.txt')
for line in fd:
print line,
fd.close()
[root@localhost 20171228]# python read_file.py
2222
3333
[root@localhost 20171228]#
使用while循环遍历文件
脚本:
#!/usr/bin/python
fd = open('/tmp/1.txt')
while True:
line = fd.readline()
if not line:
break
print line,
fd.close()
[root@localhost 20171228]# python read_fi_while.py
2222
3333
[root@localhost 20171228]#
with open打开文件 :
#!/usr/bin/python
with open('/tmp/1.txt') as fd:
while True:
line = fd.readline()
if not line:
break
print line,
打开文件
open
r:以读模式打开
w:以写模式打开
a:以追加模式打开
r+:以读写模式打开
w+:以读写模式打开(参见w)
a+:以读写模式打开(参见a)
rb:以二进制读模式打开
wb:以二进制写模式打开(参见w)
ab:以二进制追加模式打开(参见a)
rb+:以二进制读写模式打开(参见r+)
wb+:以二进制读写模式打开(参见w+)
ab+:以二进制读写模式打开(参见a+)
查看帮助:
open(...)
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
(END)...skipping...
[root@localhost ~]# cat /tmp/1.txt
1111
[root@localhost ~]#
只读方式打开:
In [26]: open('/tmp/1.txt')
Out[26]: <open file '/tmp/1.txt', mode 'r' at 0x20860c0>
In [27]: fd = open('/tmp/1.txt')
In [28]: fd
Out[28]: <open file '/tmp/1.txt', mode 'r' at 0x20861e0>
In [29]: type(fd)
Out[29]: file
以写方式打开:
In [34]: fd = open('/tmp/1.txt','w')
In [35]: fd.write('2222\n')
In [36]: fd.close()
[root@localhost ~]# cat /tmp/1.txt
2222
[root@localhost ~]#
以追加方式打开:
In [34]: fd = open('/tmp/1.txt','a')
In [35]: fd.write('3333\n')
In [36]: fd.close()
[root@localhost ~]# cat /tmp/1.txt
2222
3333
[root@localhost ~]#
read():
In [41]: fd.read()
Out[41]: '2222\n3333\n'
In [42]: fd.read()
Out[42]: ''
In [49]: fd.readline()
Out[49]: '2222\n'
In [50]: fd.readline()
Out[50]: '3333\n'
In [51]: fd.readline()
Out[51]: ''
In [52]:
read() 和readline()返回的是字符串:
readlines()返回的是列表:
in [52]: fd = open('/tmp/1.txt')
In [53]: fd.readlines()
Out[53]: ['2222\n', '3333\n']
脚本:
#!/usr/bin/python
fd = open('/tmp/1.txt')
for line in fd:
print line,
fd.close()
[root@localhost 20171228]# python read_file.py
2222
3333
[root@localhost 20171228]#
使用while循环遍历文件
脚本:
#!/usr/bin/python
fd = open('/tmp/1.txt')
while True:
line = fd.readline()
if not line:
break
print line,
fd.close()
[root@localhost 20171228]# python read_fi_while.py
2222
3333
[root@localhost 20171228]#
with open打开文件 :
#!/usr/bin/python
with open('/tmp/1.txt') as fd:
while True:
line = fd.readline()
if not line:
break
print line,
相关文章推荐
- python 表达式和语句及for、while循环练习实例
- 利用Python循环(包括while&for)各种打印九九乘法表的实例
- 复习 使用for、while循环遍历文件,数据类型转换
- 复习 使用for、while循环遍历文件,数据类型转换
- 使用for,while循环遍历文件
- python 循环while和for in简单实例
- Python循环遍历,while,for语句详细19道练习题!
- python之For/While循环遍历文件【基础篇】
- python for android : 一个简单文件浏览器的实例 play mp3
- python For 循环 三种遍历方式
- Python2与python3中 for 循环语句基础与实例分析
- js---javaScript(循环语句的使用实例)for while do....while(转移语句): break, continue , return
- 对Python 文件夹遍历和文件查找的实例讲解
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- Python详解(一)Python的基本语法,简单数据类型,和while,for循环控制,ifesle控制
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- Python for &while 循环
- Python os模块实例之遍历目录及子目录指定扩展名的文件
- python开发之for循环操作实例详解
- python中while循环语句用法简单实例