subprocess.call 在 Windows 上使用 cygwin 而不是 cmd
subprocess.call using cygwin instead of cmd on Windows
我正在 Windows 7 上编程,在我的 Python 项目之一中我需要调用 bedtools, which only works with Cygwin on Windows. I'm new to Cygwin, installed the default version + everything needed for bedtools and then used Cygwin to install bedtools by using make as described in the installation instructions.
$ tar -zxvf BEDTools.tar.gz
$ cd BEDTools-<version>
$ make
当我像下面这样使用 Cygwin 终端手动调用它时,它没有问题并且输出文件包含正确的结果。
bedtools_exe_path intersect -a gene_bed_file -b snp_bed_file -wa -wb > output_file
但是当我在我的程序中使用 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)
没有输出文件和 return 代码 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)
生成空输出文件和 return 代码 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)
没有输出文件,return 代码 126
和
D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
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)
生成空输出文件,return 代码 126
和
D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
有什么办法让它发挥作用吗?
假设您想 运行 来自 Windows 的 Linux 命令。您可以将 Linux 安装到 VM 中并通过 ssh 运行 命令(Windows 上的 Putty/plink):
#!/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
命令,允许直接 运行 命令:
cmd = [r'C:\cygwin\path\to\run.exe', '-p', '/path/to/', 'bedtools',
'-wait', 'arg1', 'arg2']
注意:在这两种情况下,Python 脚本都是 Windows 中的 运行。 bedtools
是 Linux 或 Cygwin(非 Windows)命令,因此您应该提供 POSIX 路径。
以下工作没有问题。 "
不需要转义。
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)
也可以使用以下方法:
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)
有:
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
) 而不是 bash
或 sh
是行不通的。
谢谢 eryksun、Padraic Cunningham 和 J.F。塞巴斯蒂安
我正在 Windows 7 上编程,在我的 Python 项目之一中我需要调用 bedtools, which only works with Cygwin on Windows. I'm new to Cygwin, installed the default version + everything needed for bedtools and then used Cygwin to install bedtools by using make as described in the installation instructions.
$ tar -zxvf BEDTools.tar.gz
$ cd BEDTools-<version>
$ make
当我像下面这样使用 Cygwin 终端手动调用它时,它没有问题并且输出文件包含正确的结果。
bedtools_exe_path intersect -a gene_bed_file -b snp_bed_file -wa -wb > output_file
但是当我在我的程序中使用 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)
没有输出文件和 return 代码 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)
生成空输出文件和 return 代码 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)
没有输出文件,return 代码 126
和
D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
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)
生成空输出文件,return 代码 126
和
D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file
有什么办法让它发挥作用吗?
假设您想 运行 来自 Windows 的 Linux 命令。您可以将 Linux 安装到 VM 中并通过 ssh 运行 命令(Windows 上的 Putty/plink):
#!/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
命令,允许直接 运行 命令:
cmd = [r'C:\cygwin\path\to\run.exe', '-p', '/path/to/', 'bedtools',
'-wait', 'arg1', 'arg2']
注意:在这两种情况下,Python 脚本都是 Windows 中的 运行。 bedtools
是 Linux 或 Cygwin(非 Windows)命令,因此您应该提供 POSIX 路径。
以下工作没有问题。 "
不需要转义。
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)
也可以使用以下方法:
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)
有:
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
) 而不是 bash
或 sh
是行不通的。
谢谢 eryksun、Padraic Cunningham 和 J.F。塞巴斯蒂安