Python文件处理(1)

读取文件

解决方案:

最简单的就是一次性读取所有的内容放在一个大字符串中 

all_the_text=open('thefile.txt').read()
all_the_data=open('abinfile','rb').read()

为了安全起见,最好将打开的文件对象指定一个名字,这样操作完成后可以迅速关闭文件 

file_object=open('thefile.txt')
try:
    all_the_file=file_object.read();
finally:
    file_object.close();

更简单的方法是逐行读取文本文件内容,将读取的数据放置于一个字符串列表中 

file_object=open('thefile.txt')
try:
    list_of_all_the_lines=file_object.readlines()
finally:
    file_object.close();

这样每一行的末尾都会有'\n',如果不想这样,有一些替代的方法 

list_of_all_the_lines=file_object.read().splitlines()
list_of_all_the_lines=file_object.read().split('\n')
list_of_all_the_lines=[L.rstrip('\n') for L in file_object]
最简单的逐行处理文本文件的方法是用for循环 
for line in file_object:
       print line
删除行尾的'\n'只需要添加 
line=line.rstrip('\n')

写入文件

解决方案:

最简单的方法: 

open('e://thefile.txt','w').write(all_the_text)
open('abinfile','wb').write(all_the_data)
有时候需要写入的文件不在一个大字符串中,而在一个字符串列表中,这时候需要用到writelines方法 
list_of_text_strings=['abc\n','defg\n','hijkl hahaha\n']
file_object=open('e://thefile.txt','w')
file_object.writelines(list_of_text_strings)
file_object.close()

搜索和替换文件中的文本(将某个字符串变为另一个)

解决方案:

使用字符串对象的replace方法 

import os,sys
nargs=len(sys.argv)
if not 3<=nargs<=5:
    print "usage: %s search_text repalce_text [infile [outfile]]" % \
        os.path.basename(sys.argv[0])
else:
    stext=sys.argv[1]
    rtext=sys.argv[2]
    input_file=sys.stdin
    output_file=sys.stdout
    if nargs> 3:
        input_file=open(sys.argv[3])
    if nargs> 4:
        output_file=open(sys.argv[4])
    for s in input_file:
        output_file.write(s.replace(stext,rtext))
    output_file.close()
    input_file.close()
我试验了一次,发现这里的s是读取一行进行一次替换然后将替换好的行写入文件


从文件中读取指定的行

解决方案:

使用标准库linecache模块 

import linecache
theline=linecache.getline('thefile.txt',line_number)

处理文件中每个单词

解决方案:

使用两重循环,一重处理行,一重处理单词 

for line in open('thefile.txt'):
    for word in line.split():
        print word


遍历目录树

解决方案:

使用os模块中的os.walk 

import os,fnmatch
def all_files(root,patterns='*',single_level=False,yield_folders=False):
    patterns=patterns.split(';')
    for path,subdir,files in os.walk(root):
        if yield_folders:
            files.extend(subdir)
        files.sort()
        for name in files:
            for pattern in patterns:
                if fnmatch.fnmatch(name,pattern):
                   yield os.path.join(path,name)
                   break
        if single_level:
            break

for path in all_files('e://',single_level=True):
    print path


从指定搜索路径寻找文件

解决方案:

循环指定的搜索路径中的目录 

import os
def search_file(filename,search_path,pathsep=os.pathsep):
    for path in search_path.split(pathsep):
        canditate=os.path.join(path,filename)
        if os.path.isfile(canditate):
            return os.path.abspath(canditate)
        return None
search_path='h://'
find_file=search_file('wubi*',search_path)
if find_file:
    print "File found at %s" % find_file
else:
    print "File not found"







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值