【问题标题】:string getting overwritten after concatenating with another string与另一个字符串连接后字符串被覆盖
【发布时间】:2015-01-04 22:57:18
【问题描述】:

我正在使用子进程通过 findbuild.exe 获取服务器上的构建路径。某些 exe 使用构建字符串在服务器上查找构建。 我将 findbuild.exe 的输出存储在“out”变量中。如下所示

process = subprocess.Popen(findBuild_cmd, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
out,err  = process.communicate()

我解析它以获取构建位置并使用

丢弃其余部分
sblTemp = str((out.partition("Location:       ")[2]).partition("\n")[0])
sblTemp is a network path like \\water\build\12345\123.4\

我想像 \boot\builds\bin\abc.mbn 这样添加偏移量

所以,我想要的最终路径是

\\water\build\12345\123.4\boot\builds\bin\abc.mbn

我使用了 os.path.join 但发现字符串被后面的偏移量覆盖了。 当我试图连接上述两个时,我看到如果我这样做 temp = r"XYZ"+ sbl,它工作正常,但是当我尝试这样做时 temp = sblTemp + r"XYZ" 它会覆盖第一个字符串

你能帮我弄清楚这里出了什么问题吗?

【问题讨论】:

  • 你的问题有点混乱。您能否提供一个小代码示例并告诉我们它给出的错误结果是什么?
  • sbl 具有构建路径- temp = "" temp = sbl + r"HELLO" print temp print "" temp = "" temp = r"HELLO" + sbl print temp 输出:HELLOfle\builds41 \INTEGRATION\M4004.2 你好\\waffle\builds41\INTEGRATION\M4004.2

标签: python python-2.7 subprocess string-concatenation


【解决方案1】:

我知道出了什么问题。 findBuild.exe 在字符串末尾添加了一个回车符,这搞砸了一切。当我添加 .strip 时,它工作正常:)

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2015-09-25
    • 2018-02-26
    • 2019-03-07
    • 1970-01-01
    相关资源
    最近更新 更多