Workhorse
Workhorse

Reputation: 1560

Run Python Script with variable in arguments

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!

Upvotes: 0

Views: 295

Answers (2)

Ipor Sircer
Ipor Sircer

Reputation: 3141

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

Upvotes: 2

Grayscale
Grayscale

Reputation: 1532

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.

Upvotes: 2

Related Questions