subprocess.call在Windows上使用cygwin而不是cmd

Nii*_*itz 6 python windows cygwin subprocess cmd

我在Windows 7上进行编程,在我的一个Python项目中,我需要调用bedtools,它只适用于Windows上的Cygwin.我是Cygwin的新手,安装了默认版本+ bedtools所需的一切,然后使用Cygwin按照安装说明中的说明使用make安装bedtools .

$ tar -zxvf BEDTools.tar.gz
$ cd BEDTools-<version>
$ make
Run Code Online (Sandbox Code Playgroud)

当我使用Cygwin终端手动调用它时,如下所示,它没有问题,输出文件包含正确的结果.

bedtools_exe_path intersect -a gene_bed_file -b snp_bed_file -wa -wb > output_file
Run Code Online (Sandbox Code Playgroud)

但是当我subprocess.call在我的程序中使用它似乎使用Windows cmd而不是Cygwin时,它不起作用.

arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)
Run Code Online (Sandbox Code Playgroud)

结果没有输出文件和返回码3221225781.


arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)
Run Code Online (Sandbox Code Playgroud)

导致空输出文件和返回码的结果3221225781.


cygwin_bash_path = 'D:/Cygwin/bin/bash.exe'
arguments = [cygwin_bash_path, bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)
Run Code Online (Sandbox Code Playgroud)

结果没有输出文件,返回代码为126

D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)
arguments = [cygwin_bash_path, bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)
Run Code Online (Sandbox Code Playgroud)

结果为空输出文件,返回代码为126

D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)

任何想法我怎么能让它工作?

Nii*_*itz 1

下面的工作没有问题。不需要"转义。

argument = 'sh -c \"' + bedtools_exe_path + ' intersect -a ' + gene_bed_file + 
           ' -b ' + snp_bed_file + ' -wa -wb\"'

with open(output_file, 'w') as file:
    subprocess.call(argument, stdout=file)
Run Code Online (Sandbox Code Playgroud)

也可以使用以下作品:

argument = 'bash -c \"' + bedtools_exe_path + ' intersect -a ' + gene_bed_file + 
           ' -b ' + snp_bed_file + ' -wa -wb\"'

with open(output_file, 'w') as file:
    subprocess.call(argument, stdout=file)
Run Code Online (Sandbox Code Playgroud)

和:

bedtools_exe_path = 'D:/BEDTools/bin/bedtools.exe'
gene_bed_file = 'output/gene.csv'
snp_bed_file = 'output/snps.csv'
output_file = 'output/intersect_gene_snp.bed'
Run Code Online (Sandbox Code Playgroud)

使用 cygwin bash.exe ( ) 的路径D:/Cygwin/bin/bash.exe代替bashsh不起作用。

谢谢 eryksun、Padraic Cunningham 和 JF Sebastian。