使用 bash 脚本将参数传递给 python 脚本

use bash script to pass arguments to a python script

我有一个非常基本的问题,但我对python的了解非常有限。我有一个 python 脚本,它需要几个参数才能 运行 (https://github.com/raphael-group/hotnet2/blob/master/bin/createPPRMat.py)。

我想使用带有一些文件名(每行一个)的文件作为传递给 python 脚本的第一个参数。

我的第一个尝试是创建一个 bash 脚本 (mat.sh),如下所示:

#!/bin/bash
for net in $(cat /home/hotnet2-1.0.0/iref/iref.list);
do
export
python createPPRMat.py -e `$net` -i /home/jfertaj/hotnet2-1.0.0/iref/iref_index_genes -o /home/jfertaj/Broad_Stay/hotnet2-1.0.0/iref_influence_matrices
done

但是我得到一个错误,python 脚本似乎没有解析 $net 变量:

createPPRMat_1.py: error: argument -e/--edgelist_file: expected one argument
mat.sh: line 6: /home/jfertaj/Broad_Stay/hotnet2-1.0.0/iref/iref_edgelist_139: No such file or directory

当我在 bash 脚本 ("$net") 中双引号变量 net 时,我得到的错误是不同的,指出文件名有问题

Traceback (most recent call last):
File "/home/jfertaj/Broad_Stay/hotnet2-1.0.0/bin/createPPRMat_1.py", line 96, in <module>
run(get_parser().parse_args(sys.argv[1:]))
File "/home/hotnet2-1.0.0/bin/createPPRMat_1.py", line 38, in run
edges = [map(int, l.rstrip().split()[:2]) for l in open(args.edgelist_file)]
IOError: [Errno 2] No such file or directory: '\x1b[01;00m/home/hotnet2-1.0.0/iref/iref_edgelist_164\x1b[0m'

iref.list的内容是这样的:

/home/hotnet2-1.0.0/iref/iref_edgelist_1
/home/hotnet2-1.0.0/iref/iref_edgelist_10
/home/hotnet2-1.0.0/iref/iref_edgelist_100

并且 iref.list 文件是使用 cat -1 ... < iref.list

创建的

任何帮助将不胜感激

谢谢

python 回溯向您展示了问题(正如您所注意到的)。

Traceback (most recent call last):
File "/home/jfertaj/Broad_Stay/hotnet2-1.0.0/bin/createPPRMat_1.py", line 96, in <module>
run(get_parser().parse_args(sys.argv[1:]))
File "/home/hotnet2-1.0.0/bin/createPPRMat_1.py", line 38, in run
edges = [map(int, l.rstrip().split()[:2]) for l in open(args.edgelist_file)]
IOError: [Errno 2] No such file or directory: '\x1b[01;00m/home/hotnet2-1.0.0/iref/iref_edgelist_164\x1b[0m'

该文件不是文本文件。它是一个二进制文件。它包含文件名和 shell 颜色代码。在逐字使用文件名之前,您需要删除(或过滤掉)这些颜色代码(或者获取文件的干净副本并修复任何进程将颜色代码吐出到文件以停止这样做)。

使用反引号时出现的不同错误(缺少参数)是因为反引号 运行 它们的内容作为命令。所以 `$net` 获取 $net 变量的值并尝试 运行 它作为 shell 命令,然后用该命令的输出替换整个反引号引用的字符串。

这就是为什么你在那里得到 "no such file or directory" 错误(因为文件名和代码无效),随后,为什么 -e 标志没有参数(反引号字符串评估为空字符串,因此您最终得到 -e -i 并且 -e).

没有参数