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 '\n'.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:\Users\yuxinglx\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat"\n'''
add_str2 = "    return vcvarsall\n\n"
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"%s\Setup\VC" % vsbase,
                                   "productdir")
    except KeyError:
        productdir = None

    # trying Express edition
    if productdir is None:
        vsbase = VSEXPRESS_BASE % version
        try:
            productdir = Reg.get_value(r"%s\Setup\VC" % 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:\Users\yuxinglx\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值