Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash脚本向python脚本传递参数_Python_Bash_For Loop - Fatal编程技术网

使用bash脚本向python脚本传递参数

使用bash脚本向python脚本传递参数,python,bash,for-loop,Python,Bash,For Loop,我有一个非常基本的问题,但我对python的了解非常有限。我有一个python脚本,它需要几个参数才能运行() 我想使用带有一些文件名(每行一个)的文件作为传递给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/jfe

我有一个非常基本的问题,但我对python的了解非常有限。我有一个python脚本,它需要几个参数才能运行()

我想使用带有一些文件名(每行一个)的文件作为传递给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
/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
但是我遇到了一个错误,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'
使用
cat-1…

任何帮助都将不胜感激


谢谢

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

回溯(最近一次呼叫最后一次):
文件“/home/jfertaj/Broad_Stay/hotnet2-1.0.0/bin/createPPRMat_1.py”,第96行,在
运行(get_parser().parse_args(sys.argv[1:]))
文件“/home/hotnet2-1.0.0/bin/createPPRMat_1.py”,第38行,运行中
edges=[map(int,l.rstrip().split()[:2]),用于打开的l(args.edgelist_文件)]
IOError:[Errno 2]没有这样的文件或目录:'\x1b[01;00m/home/hotnet2-1.0.0/iref/iref\U edgelist\U 164\x1b[0m'
该文件不是文本文件。它是一个二进制文件。它包含文件名和外壳颜色代码。您需要删除(或过滤掉)这些颜色代码,然后才能逐字使用文件名(或者获取文件的干净副本,并修复向文件吐出颜色代码的任何进程,以停止这样做)

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


这就是为什么会出现“没有这样的文件或目录”错误(因为带有代码的文件名无效)以及随后为什么
-e
标志没有参数(反勾选字符串计算为空字符串,因此您最终得到
-e-i
并且
-e
没有参数)。

\x1b[01;00m/home/hotnet2-1.0.0/iref/iref_edgelist_164\x1b[0m'
输出中的这个看起来像是文件中有颜色控制代码。这不是文本文件。你需要得到一个干净的文本文件,或者去掉这些文件,然后才能工作。好的,我理解颜色控制代码,但是为什么我使用
$net
时,我得到的错误不同?嗯,也许这是一个非常愚蠢的问题…背景标记s将其内容作为命令运行。因此,
`$net`
获取
$net
变量的值,并尝试将其作为shell命令运行,并用命令的输出替换backtick字符串。这就是为什么会出现“没有这样的文件或目录”错误以及
-e
参数没有参数的原因(反勾选字符串的计算结果为空字符串)。感谢@EtanReisner提供的帮助和信息,删除控制代码成功了。如果您发布的评论是一个答案,我将非常乐意为您打分
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'