【问题标题】:Copy lines between two specific strings using Python when subprocess is involved涉及子进程时,使用 Python 在两个特定字符串之间复制行
【发布时间】:2018-03-21 23:18:20
【问题描述】:

我想将 file1.v 中的特定代码行(两个特定行之间的代码)附加到 file2.sv 中

我正在尝试借助 subprocess 模块从 python 运行外部 perl 脚本。这个外部 perl 脚本为我创建了“file2.sv”。 file1.v 已经存在。

我的问题是 file2.sv 是由 perl 脚本成功创建的,但是我试图从 file1.v 将行附加到 file2.sv 的代码不起作用

代码如下:

pipe = subprocess.Popen(
    ["Verilog.pl", "file1.v", "file2.sv", "0", "SystemVerilog", "SV","0"], stdin=subprocess.PIPE)

with open("file1.v","r") as rf_VamsModel, open("file2.sv", "a") as wf_sytemVerilogFile:
    copy = False
    for line in rf_VamsModel:
        if line.strip() == "//Start of functional specification here":
            copy = True
        elif line.strip() == "//End of functional specification here":
            copy = False
        elif copy:
            print("line test2={}".format(line))
            wf_sytemVerilogFile.write(line)

是不是因为 subprocess.Popen 和 file append 进程是并行执行的?

【问题讨论】:

    标签: python subprocess


    【解决方案1】:

    Popen 构造函数启动一个进程,但它不会等到它完成就返回。您很可能在 perl 脚本完成对这两个文件的写入之前尝试读取和写入它们。

    您可以通过调用 wait() 来等待 perl 脚本完成

    process = subprocess.Popen(...)
    process.wait()
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2018-09-06
      • 2021-03-03
      • 2012-07-28
      • 2020-03-23
      • 2021-11-06
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多