Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a Bash variable to Python?

Eventually I understand this and it works.

bash script:

#!/bin/bash
#$ -V
#$ -cwd
#$ -o $HOME/sge_jobs_output/$JOB_ID.out -j y
#$ -S /bin/bash
#$ -l mem_free=4G


c=$SGE_TASK_ID
cd /home/xxx/scratch/test/
FILENAME=`head -$c testlist|tail -1`
python testpython.py $FILENAME

python script:

#!/bin/python
import sys,os


path='/home/xxx/scratch/test/'
name1=sys.argv[1]
job_id=os.path.join(path+name1)
f=open(job_id,'r').readlines()
print f[1]

thx

like image 731
LookIntoEast Avatar asked Sep 22 '11 20:09

LookIntoEast


People also ask

How do I pass a variable from bash to Python?

Your Python script is a child process, so you need to make sure that you export the variable you want the child process to access. To answer your original question, you need to first export the variable and then access it from within the python script using os. environ . Save this answer.

How do I convert a bash command to Python?

Executing bash scripts using Python subprocess module We can do it by adding optional keyword argument capture_output=True to run the function, or by invoking check_output function from the same module. Both functions invoke the command, but the first one is available in Python3.

Can I use bash in Python?

Bash is an implementation of the shell concept and is often used during Python software development as part of a programmer's development environment. Bash is an implementation of the shells concept.


2 Answers

Exported bash variables are actually environment variables. You get at them through the os.environ object with a dictionary-like interface. Note that there are two types of variables in Bash: those local to the current process, and those that are inherited by child processes. Your Python script is a child process, so you need to make sure that you export the variable you want the child process to access.

To answer your original question, you need to first export the variable and then access it from within the python script using os.environ.

##!/bin/bash
#$ -V
#$ -cwd
#$ -o $HOME/sge_jobs_output/$JOB_ID.out -j y
#$ -S /bin/bash
#$ -l mem_free=4G

c=$SGE_TASK_ID
cd /home/xxx/scratch/test/
export FILENAME=`head -$c testlist|tail -1`
chmod +X testpython.py
./testpython.py


#!/bin/python
import sys
import os

for arg in sys.argv:  
    print arg  

f=open('/home/xxx/scratch/test/' + os.environ['FILENAME'],'r').readlines()
print f[1]

Alternatively, you may pass the variable as a command line argument, which is what your code is doing now. In that case, you must look in sys.argv, which is the list of arguments passed to your script. They appear in sys.argv in the same order you specified them when invoking the script. sys.argv[0] always contains the name of the program that's running. Subsequent entries contain other arguments. len(sys.argv) indicates the number of arguments the script received.

#!/bin/python
import sys
import os

if len(sys.argv) < 2:
    print 'Usage: ' + sys.argv[0] + ' <filename>'
    sys.exit(1)

print 'This is the name of the python script: ' + sys.argv[0]
print 'This is the 1st argument:              ' + sys.argv[1]

f=open('/home/xxx/scratch/test/' + sys.argv[1],'r').readlines()
print f[1]
like image 110
Nathan Avatar answered Sep 24 '22 16:09

Nathan


use this inside your script (EDITED per Aarons suggestion):

def main(args):
    do_something(args[0])


if __name__ == "__main__":
    import sys
    main(sys.argv[1:])
like image 32
MattoTodd Avatar answered Sep 21 '22 16:09

MattoTodd