简体   繁体   English

在Python中的每个循环迭代中,使用结果文件进行子流程调用

[英]With every loop iteration in Python use resulting file for subprocess call

I have one input file (inputFile) that needs to undergo six filtering steps with six different 'BED' files. 我有一个输入文件(inputFile),需要对六个不同的“ BED”文件进行六个过滤步骤。 After every single filter, there results a new input file. 在每个单个过滤器之后,将产生一个新的输入文件。 I need to use these for input to the next iteration of my loop and so on until the initial file has undergone six iterations with all six of my BED files. 我需要将它们用作循环的下一个迭代的输入,依此类推,直到初始文件与所有六个BED文件都经历了六个迭代为止。

Here is my current code. 这是我当前的代码。 The loop works for the first two calls but then stops and says that it cannot open the file that got created after the second iteration. 该循环适用于前两个调用,但随后停止并表示无法打开第二次迭代后创建的文件。 Any advice is greatly appreciated. 任何意见是极大的赞赏。

samples = [0, 1, 2, 3, 4, 5]
fileName = ["first", "second", "third", "fourth", "fifth"]
inputFile = fileOne
for x in samples:
   orderName = fileName[x] + sampleCapture + ".bed"
   outputFile = open(orderName, "w")
   bedFile = filterFiles[x]
   subprocess.Popen(["bedtools", "intersect", "-a", inputFile, "-b", bedFile, "-v"], 
                    stdout=outputFile)
   outputFile.close()
   inputFile = fileName[x] + sampleCapture + ".bed"

Popen returns immediately. Popen立即返回。 You should use check_call() instead (as @cel mentioned ): 您应该改用check_call() (如@cel所述 ):

from subprocess import check_call

input_filename = "fileone"
for filename, bed_filename in zip(["first", "second", "third", "fourth", "fifth"],
                                  filter_files):
  args = ["bedtools", "intersect", "-a", input_filename, "-b", bed_filename, "-v"]
  output_filename = filename + sample_capture + ".bed"
  with open(output_filename, "wb", 0) as output_file:
    check_call(args, stdout=output_file)
  input_filename = output_filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM