user2380782
user2380782

Reputation: 1446

use bash script to pass arguments to a python script

I have a very basic problem, but my knowledge about python is very limited. I have a python script that takes several arguments in order to run (https://github.com/raphael-group/hotnet2/blob/master/bin/createPPRMat.py).

I would like to use a file with some filenames (one per line) as the first argument to be passed to the python script.

My first try to do it was creating a bash script (mat.sh) like this:

#!/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

However I got an error, the python script seems to not parse the $net variable:

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

When I double quote the variable net in the bash script ("$net") the error I got is different, pointing out that something is wrong with the filename

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'

The content of iref.list looks like this:

/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

And the iref.list file was created using cat -1 ... < iref.list

Any help would be very appreciated

Thanks

Upvotes: 0

Views: 378

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

The python traceback shows you the problem (as you noticed).

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'

The file is not a text file. It is a binary file. It contains file names and shell color codes. You need to remove (or filter out) those color codes before you can use the filenames verbatim (alternatively get a clean copy of the file and fix whatever process spits out color codes to files to stop doing that).

The different error (missing argument) you get when you use the backticks is because the backticks run their contents as a command. So `$net` takes the value of the $net variable and tries to run it as a shell command and then replaces the entire backtick quoted string with the output of that command.

That's why you get the "no such file or directory" error there (because the filename, with codes, is not valid) and, subsequently, why the -e flag doesn't have an argument (the backtick string evaluated to the empty string so you end up with -e -i and no argument to -e).

Upvotes: 2

Related Questions