python读取文件的方法是什么_python 读取文件的常用方法

[root@localhost hejoy]# cat readfile.py

#/usr/bin/env python

#-*-coding:UTF-8-*-

'''

练习读文件,获取关键语句

2017-04-14 09:33

hejoy

'''

import sys

import types

"""读取文件的常用方法"""

def methodone():

fd = open("/home/userhome/weihengjun/20170124.txt")

try:

for line in fd.readlines():

print line

except IOError:

print e

finally:

fd.close()

def methodtwo():

try:

for line in open("/home/userhome/weihengjun/20170124.txt"):

print line

except IOError:

print e

'''

检查数据中是否含有指定的字符,然后输出;

find的用法,如果不含有指定的字符,则返回对应的索引,否则返回-1

'''

def checkstring():

fd = open("/home/userhome/weihengjun/20170123.txt")

for line in fd.readlines():

aa=line.find("2")

if aa == -1:

pass

else:

print "Is Here "+line

def main():

#methodtwo()

checkstring()

if __name__=='__main__':

main()

[root@localhost hejoy]# python readfile.py

Is Here ÿþ18269751

Is Here 150211782

Is Here 177933502

Is Here 15021267

Is Here 18040824

Is Here 13990244

Is Here 18930298

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值