Run Python Script with variable in arguments

Workhorse Source

I have a python command that runs as follows:

python script.py -file 1000G_EUR_Phase3_plink/1000G.NUMBER --out GTEx_Cortex.chrNUMBER

I would like to replace the NUMBER variable with the numbers 1:20. So if I replace NUMBER with 1 it would look like this:

python script.py -file 1000G_EUR_Phase3_plink/1000G.1 --out GTEx_Cortex.chr1

and this on the second iteration (if I replace it with 2):

python script.py -file 1000G_EUR_Phase3_plink/1000G.2 --out GTEx_Cortex.chr2

But I don't want to keep manually changing NUMBER 20 times. I want to automate the entire thing.

How can I do this in the command prompt? Should this be done in VIM or is there another way in python?

Thanks!

pythonlinuxpython-2.7shell

Answers

answered 1 week ago Ipor Sircer #1

for i in `seq 1 20`;do python script.py -file 1000G_EUR_Phase3_plink/1000G.${i} --annot GTEx_Cortex_chr1.annot.gz --out GTEx_Cortex.chr${i};done

answered 1 week ago Grayscale #2

If you are doing this frequently you could also write a bash script.

Create a file run_stuff that loops through commands. It could be analogous to this:

#!/bin/bash
n=$1
i=1
while (( i <= n )); do
    python prog${i}.py
    (( i = i + 1 ))
done

The above script runs prog1.py, then prog2.py, and so on. For your code, just replace the 5th line with the analogous line you want.

Then in the terminal you would do:

chmod u+x run_stuff
./run_stuff 20

The chmod command just changes the permissions of the file so you can execute it.

comments powered by Disqus