0

I'm trying to write a script in python to make my job easier. I need to use os.system to call some functions to an external software.

Is there a way to insert a for loop inside this string, without having to write obs_dir[n] every time??

import os
obs_dir = ['18185','18186','18187','19926','19987','19994','19995','20045','20046','20081']
xid = ['src21']
i=0

os.system("pset combine_spectra src_arfs=/"
          + obs_dir[0] + "/" + xid[i] + "_" + obs_dir[0] + "_spectrum.arf,"
          + "/" + obs_dir[1] + "/" + xid[i] + "_" + obs_dir[1] + "_spectrum.arf,"
          + "/" + obs_dir[2] + "/" + xid[i] + "_" + obs_dir[2] + "_spectrum.arf,"
          + "/" + obs_dir[3] + "/" + xid[i] + "_" + obs_dir[3] + "_spectrum.arf,"
          + "/" + obs_dir[4] + "/" + xid[i] + "_" + obs_dir[4] + "_spectrum.arf,"
          + "/" + obs_dir[5] + "/" + xid[i] + "_" + obs_dir[5] + "_spectrum.arf,"
          + "/" + obs_dir[6] + "/" + xid[i] + "_" + obs_dir[6] + "_spectrum.arf,"
          + "/" + obs_dir[7] + "/" + xid[i] + "_" + obs_dir[7] + "_spectrum.arf,"
          + "/" + obs_dir[8] + "/" + xid[i] + "_" + obs_dir[8] + "_spectrum.arf,"
          + "/" + obs_dir[9] + "/" + xid[i] + "_" + obs_dir[9] + "_spectrum.arf")
3
  • insert a for loop inside this string... You mean a loop around the string Commented Feb 23, 2018 at 11:02
  • I need to run os.system(...) only once. Commented Feb 23, 2018 at 11:04
  • 1
    Make the string first, then use it Commented Feb 23, 2018 at 11:10

4 Answers 4

3

You can create the required command by first iterating over the list(obs_dir) and forming the string.

Ex:

import os
obs_dir = ['18185','18186','18187','19926','19987','19994','19995','20045','20046','20081']
xid = ['src21']


s = "pset combine_spectra src_arfs="
for i in obs_dir:
    s += "/{0}/{1}_{0}_spectrum.arf, ".format(i, xid[0])

s = s.strip().rstrip(',')
print s
#os.system(s)
Sign up to request clarification or add additional context in comments.

Comments

2

I think this might be what you want

import os
obs_dir = ['18185','18186','18187','19926','19987','19994','19995','20045','20046','20081']
xid = ['src21']

str_cmd = "pset combine_spectra src_arfs=" + obs_dir[0]
separator = ""
for dir in obs_dir
  str_cmd + =  separator + "/" + dir + "/" + xid[i] + "_" + dir + "_spectrum.arf"
  separator = ","
  
os.system(str_cmd)

Comments

2

You have xid[i], but no i, so using xid[0],

"/{}/{}_{}_spectrum.arf".format(obs_dir[1],xid[0],obs_dir[1])

gives

'/18186/src21_18186_spectrum.arf'

So, format helps. Also, join will help join these into a comma separated string:

",".join(['a', 'b'])

gives

'a,b'

Joining this together you get

s = ",".join(["/{}/{}_{}_spectrum.arf".format(o,xid[0],o) for o in obs_dir])

giving the parameter(s) you want

'/18185/src21_18185_spectrum.arf,/18186/src21_18186_spectrum.arf,/18187/src21_18g187_spectrum.arf,/19926/src21_19926_spectrum.arf,/19987/src21_19987_spectrum.arfg,/19994/src21_19994_spectrum.arf,/19995/src21_19995_spectrum.arf,/20045/src21_20g045_spectrum.arf,/20046/src21_20046_spectrum.arf,/20081/src21_20081_spectrum.arfg'

without a spare ',' on the end.

Then use it

os.system("pset combine_spectra src_arfs=" + s)

Comments

1

Not in the string, but we can build the string using features like list comprehension (in this case, a generator expression) and string joining:

obs_dir = ['18185','18186','18187','19926','19987','19994','19995','20045','20046','20081']
xid = ['src21']
i = 0

print("pset combine_spectra src_arfs=" +
    ",".join("/{0}/{1}_{0}_spectrum.arf".format(n,xid[i])
             for n in obs_dir))

Comments

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.