首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Windows上使用cygwin代替cmd的subprocess.call

在Windows上使用cygwin代替cmd的subprocess.call
EN

Stack Overflow用户
提问于 2015-07-04 13:40:45
回答 2查看 4.8K关注 0票数 7

我正在Windows 7上编程,在我的一个Python项目中,我需要调用床头工具,它只在Windows上使用Cygwin。我是Cygwin的新手,安装了默认版本+床工具所需的一切,然后使用Cygwin通过使用安装说明中描述的make安装床头工具。

代码语言:javascript
复制
$ tar -zxvf BEDTools.tar.gz
$ cd BEDTools-<version>
$ make

当我使用Cygwin终端像下面这样手动调用它时,它没有问题,输出文件包含正确的结果。

代码语言:javascript
复制
bedtools_exe_path intersect -a gene_bed_file -b snp_bed_file -wa -wb > output_file

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

代码语言:javascript
复制
arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)

结果没有输出文件和3221225781的返回代码。

代码语言:javascript
复制
arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)

结果是一个空的输出文件和一个3221225781的返回代码。

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
代码语言:javascript
复制
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)

结果是一个空的输出文件,返回代码为126

代码语言:javascript
复制
D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file

我有什么办法让它起作用吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-05 12:02:18

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

代码语言:javascript
复制
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)

还使用了以下工作:

代码语言:javascript
复制
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)

通过以下方式:

代码语言:javascript
复制
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'

使用到cygwin bash.exe (D:/Cygwin/bin/bash.exe)的路径而不是使用bashsh是行不通的。

谢谢你,艾尔克孙,帕德拉克·坎宁安和J.F.塞巴斯蒂安。

票数 1
EN

Stack Overflow用户

发布于 2015-07-04 19:40:54

假设您想要从Windows运行Linux命令。您可以将Linux安装到VM中,并通过ssh (Windows上的Putty/plink)运行命令:

代码语言:javascript
复制
#!/usr/bin/env python
import subprocess

cmd = [r'C:\path\to\plink.exe', '-ssh', 'user@vm_host', '/path/to/bedtools']
with open('output', 'wb', 0) as file:
    subprocess.check_call(cmd, stdout=file)

Cygwin提供了允许直接运行命令的run命令:

代码语言:javascript
复制
cmd = [r'C:\cygwin\path\to\run.exe', '-p', '/path/to/', 'bedtools', 
        '-wait', 'arg1', 'arg2']

注意:在这两种情况下,Python脚本都是从Windows运行的。bedtools是Linux或Cygwin (非Windows)命令,因此您应该提供POSIX路径。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31221279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档