python文件读取,替换(带格式,python lib 库)

import os, time
import sys
import re


def read_old_part(filename, start, end):
    content = []
    recording = False
    with open(filename) as f:
        for line in f:
            line = line.strip()
            if line == end:
                break
            if recording:
                content.append(line)
            if line == start:
                recording = True
    #    return '
'.join(content)
    return content


def read_all_part(filename):
    with open(filename) as f:
        lines = f.readlines()
    return lines


def read_all_part_strip(filename):
    with open(filename) as f:
        all_part = []
        for line in f:
            line = line.strip()
            all_part.append(line)
    return all_part

filename = "test.log"
start = 'def find_vcvarsall(version):'
end = 'def query_vcvarsall(version, arch="x86"):'
add_str1 = '''    vcvarsall = r"C:UsersyuxinglxAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0vcvarsall.bat"
'''
add_str2 = "    return vcvarsall

"
old_str = read_old_part(filename, start, end)
all_str = read_all_part(filename)
add_str_strip = read_all_part_strip(filename)
old_str_nu = len(old_str)
all_str_nu = len(all_str)
all_str_strip_nu = len(add_str_strip)

if all_str_nu == all_str_strip_nu:
    for line in old_str:
        if line in add_str_strip:
            all_str_strip_nu = add_str_strip.index(old_str[0])

    del all_str[all_str_strip_nu:(old_str_nu + all_str_strip_nu)]
    all_str_nu = add_str_strip.index(start)
    all_str.insert(all_str_nu + 1, add_str1)
    all_str.insert(all_str_nu + 2, add_str2)
    f = open('test.txt', 'w')
    f.writelines(all_str)
    f.close()
else:
    print "it's worng"

 test.log

        if i not in newList:
            newList.append(i)
    newVariable = os.pathsep.join(newList)
    return newVariable

def find_vcvarsall(version):
    """Find the vcvarsall.bat file

    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    """
    vsbase = VS_BASE % version
    try:
        productdir = Reg.get_value(r"%sSetupVC" % vsbase,
                                   "productdir")
    except KeyError:
        productdir = None

    # trying Express edition
    if productdir is None:
        vsbase = VSEXPRESS_BASE % version
        try:
            productdir = Reg.get_value(r"%sSetupVC" % vsbase,
                                       "productdir")
        except KeyError:
            productdir = None
            log.debug("Unable to find productdir in registry")

    if not productdir or not os.path.isdir(productdir):
        toolskey = "VS%0.f0COMNTOOLS" % version
        toolsdir = os.environ.get(toolskey, None)

        if toolsdir and os.path.isdir(toolsdir):
            productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
            productdir = os.path.abspath(productdir)
            if not os.path.isdir(productdir):
                log.debug("%s is not a valid directory" % productdir)
                return None
        else:
            log.debug("Env var %s is not set or invalid" % toolskey)
    if not productdir:
        log.debug("No productdir found")
        return None
    vcvarsall = os.path.join(productdir, "vcvarsall.bat")
    if os.path.isfile(vcvarsall):
        return vcvarsall
    log.debug("Unable to find vcvarsall.bat")
    return None

def query_vcvarsall(version, arch="x86"):
    """Launch vcvarsall.bat and read the settings from its environment
    """
    vcvarsall = find_vcvarsall(version)
    interesting = set(("include", "lib", "libpath", "path"))
    result = {}

    if vcvarsall is None:
        raise DistutilsPlatformError("Unable to find vcvarsall.bat")
    log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version)
    popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    try:

  test.txt

        if i not in newList:
            newList.append(i)
    newVariable = os.pathsep.join(newList)
    return newVariable

def find_vcvarsall(version):
    vcvarsall = r"C:UsersyuxinglxAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0cvarsall.bat"
    return vcvarsall
def query_vcvarsall(version, arch="x86"): """Launch vcvarsall.bat and read the settings from its environment """ vcvarsall = find_vcvarsall(version) interesting = set(("include", "lib", "libpath", "path")) result = {} if vcvarsall is None: raise DistutilsPlatformError("Unable to find vcvarsall.bat") log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version) popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch), stdout=subprocess.PIPE, stderr=subprocess.PIPE) try:

  

原文地址:https://www.cnblogs.com/anita-harbour/p/9258091.html