zoukankan      html  css  js  c++  java
  • 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();

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

    list_of_all_the_lines=file_object.read().splitlines()
    list_of_all_the_lines=file_object.read().split('
    ')
    list_of_all_the_lines=[L.rstrip('
    ') for L in file_object]

    最简单的逐行处理文本文件的方法是用for循环 

    for line in file_object:
           print line

    删除行尾的' '只需要添加 

    line=line.rstrip('
    ')

    写入文件

    解决方案:

    最简单的方法: 

    open('e://thefile.txt','w').write(all_the_text)
    open('abinfile','wb').write(all_the_data)

    有时候需要写入的文件不在一个大字符串中,而在一个字符串列表中,这时候需要用到writelines方法 

    list_of_text_strings=['abc
    ','defg
    ','hijkl hahaha
    ']
    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"







  • 相关阅读:
    maven-scm-plugin: Add a tag into Mercurial. 在Mercurial中添加一个tag
    JSch
    docker-compose install
    如何删除Dead状态的container
    inux下如何添加一个用户并且让用户获得root权限
    Log4j2 自定义 Appender
    Harbor私有镜像仓库(上)
    docker基础(下)
    docker基础(上)
    Pipeline流水线JAVA项目发布
  • 原文地址:https://www.cnblogs.com/riskyer/p/3318160.html
Copyright © 2011-2022 走看看