0

I am trying to run a batch process using array in slurm. I only know shell command to extract variable from array (text files), but failed to assign it as Python variable.

I have to assign a variable to a Python slurm script. I used a shell command to extract values from the array. but facing errors while assigning it to the variable. I used subprocess, os.system and os.popen. or is there any way to extract values from text file to be used as a Python variable?

start_date = os.system('$(cat startdate.txt | sed -n ${SLURM_ARRAY_TASK_ID}p)')

start_date = subprocess.check_output("$(cat startdate.txt | sed -n ${SLURM_ARRAY_TASK_ID}p)", shell=True)

start_date = os.popen('$(cat startdate.txt | sed -n ${SLURM_ARRAY_TASK_ID}p)').read()


start_date = '07-24-2004'

2 Answers 2

1

Don't use $(...). That will execute the command, and then try to execute the output of the command. You want the output to be sent back to python, not re-executed by the shell.

start_date = subprocess.check_output("cat startdate.txt | sed -n ${SLURM_ARRAY_TASK_ID}p", shell=True)
Sign up to request clarification or add additional context in comments.

Comments

0

Barmar is correct, the $(...) part is why you are not getting what you want, but the real question is why when you are using python would you want to use cat and sed as well. Just open the file and pull out the information you want

import os
with open("startdate.txt", "r") as fh:
    lines = fh.readlines()
start_date = lines[os.environ['SLURM_ARRAY_TASK_ID']].strip()

the .strip() part gets rid of the newline character.

3 Comments

Thanks Malcolm. but I got this error: NameError: name 'SLURM_ARRAY_TASK_ID' is not defined. Is 'SLURM_ARRAY_TASK_ID' should be the column name in text file?
SLURM_ARRAY_TASK_ID is only set if you submit the SLURM job as an array (i.e. with --array)
Sorry, for the delay. I missed that in your example ${SLURM_ARRAY_TASK_ID} is actually getting expanded from the environment variables in the execution of your command string. To get that in your python script you need to pull it from os.environ. I will edit my code above to show how to do that as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.