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

用户名

我有一个非常基本的问题,但是我对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

当我net在bash脚本("$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

任何帮助将不胜感激

谢谢

伊坦·赖斯纳(Etan Reisner)

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'

该文件不是文本文件。它是一个二进制文件。它包含文件名和外壳颜色代码。您需要先删除(或过滤掉)这些颜色代码,然后才能逐字使用文件名(或者获取文件的完整副本,并修复任何将颜色代码吐出到文件中的过程,以停止这样做)。

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

这就是为什么在那里出现“没有这样的文件或目录”错误的原因(因为带有文件名的文件名无效),随后,为什么-e标志没有参数(反引号字符串被评估为空字符串,因此您最终以,-e -i而没有自变量-e)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章