Python-使用for循环遍历文件

open操作文件

r:    以读方式打开,默认就是这个模式

w:   以写方式打开

a:    以追加模式打开

r+:   以读写模式打开

w+:  以读写模式打开

a+:   以读写模式打开

rb:    以二进制读模式打开

wb:   以二进制写模式打开

ab:    以二进制追加模式打开

rb+   以二进制读写模式打开

wb+  以二进制读写模式打开

ab+   以二进制读写模式打开

写入文件后,一定要执行close关闭文件

fd = open(‘/tmp/tmp.txt‘,‘w‘)

读文件

fd = open(‘/tmp/tmp.txt‘,‘r‘)

fd.read()         从头读到位,读完后指针指向文件的末尾,返回的是字符。

fd.read(2)        表示指针从开始读到第二位

fd.readline()   表示每次只读一行,返回的是字符。

fd.readlines()  得到的是所有内容,返回的是一个列表,是写在内存里的。

#!/usr/bin/python

fd = open(‘/tmp/tmp.txt‘)

for line in fd:                          //不建议后面加readlines,节约资源。

print line,

使用while循环遍历文件

#!/usr/bin/python


fd = open(‘/tmp/tmp.txt‘)

while True:

    line = fd.readline()

    if not line:

        break

    print line,

fd.close()

with open           //在python2.6以后的版本才支持

#!/usr/bin/python

with open(‘/tmp/tmp.txt‘) as fd:

    while Ture:

        line = fd.readline()

        if not line:

            break

        print line,

使用with open时,程序代码执行完以后程序会自动关闭文件。

时间: 2024-08-05 13:18:59

Python-使用for循环遍历文件的相关文章

Python中的用for,while循环遍历文件实例

使用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

使用while循环遍历文件

/* 使用while循环遍历文件*/ [[email protected] test1]# vim 17.py //add #!/usr/bin/python ll = open('/tmp/1.txt') while True: line = ll.readline() if not line: break print line, [[email protected] test1]# python 17.py abc sjdh /* 另外一种写法,(常用),省去了去把变量关闭 */ [[ema

复习 使用for、while循环遍历文件,数据类型转换

1. python访问文件 通过内置函数open打开文件,看一下open函数解释: 打开文件 返回的是一个文件对象 fd = open('/tmp/tmp.txt') 查看他的方法: 关闭文件: fd.close() 向文件内写入: fd = open('/tmp/tmp.txt', 'w') 这样打开文件等于重写文件,文件原有的内容就覆盖掉了 fd.write("a") 这样写入文件,写入文件后不能查看到,我们必须把他关闭: fd.close() 关闭之后,就可以查看到文件内的信息了

Python的list循环遍历中,删除数据的正确方法

在遍历list,删除符合条件的数据时,总是报异常,代码如下: 1 num_list = [1, 2, 3, 4, 5] 2 print(num_list) 3 4 for i in range(len(num_list)): 5 if num_list[i] == 2: 6 num_list.pop(i) 7 else: 8 print(num_list[i]) 9 10 print(num_list) 会报异常:IndexError: list index out of range 原因是在删

第十六讲 循环遍历文件和元组

for和while退出循环时,执行else语句 元组(tuples):圆括号括起来,逗号间隔,数据类型可以相同,也可以是不同类型. 元组和列表的区别:元组可以修改,列表不可以修改 eg1: tup=(1,2,3,4,5)for t in tup:    print t else: print 'out for' eg2: 查看帮助: >>> help(file.read)-----全部读入,返回的是字符串string >>> help(file.readline)-读文

C# static方法-使用迭代器循环遍历文件中的额行

//封装的方法 //读取文件的值,放入集合中 public static IEnumerable<string> ReadLines(string fileName) { using (TextReader reader=File.OpenText(fileName)) { string line; while ((line=reader.ReadLine())!=null) { yield return line; } } } //调用 class Program { static void

python 按照自然数排序遍历文件 python os.listdir sort by natural sorting

import os import re def sorted_aphanumeric(data): convert = lambda text: int(text) if text.isdigit() else text.lower() alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] return sorted(data, key=alphanum_key) file = sorted_ap

Python封装一个函数接受文件夹的名称作为输入参数,打印该文件夹中的的全部路程信息(遍历路径)

Python时间简单的遍历文件夹路径,代码如下:import os def bianli(path):info = os.listdir(path)for v in info:p = os.path.join(path, v)print(p)if os.path.isdir(p):bianli(p)bianli('D:/重命名') 实现效果如下: 原文地址:http://blog.51cto.com/13241097/2115031

python遍历文件进行数据处理

背景 之前写过一个遍历文件夹进行处理的Python程序,但因为时间太久找不着了.. 导致只能自己再写一遍,于是决定将代码放置于博客之中,以便以后使用. #!usr/bin/env python #-*- coding:utf-8 -*- import math import os import glob import numpy as np import jieba import string import jieba.analyse def read_from_file(directions)