1

What I have:

x = 0
while x <= image_number:
        x = x + 1
        (a%d % x)  = pyfits.open("final_processed%d.fit" % x)

What I want:

To set variables (in this case, a1, a2, a3 etc.) = the opened images via Pyfits to numbers ranging from 1 - # of images in the directory. So basically:

a1 = pyfits.open("final_processed1.fit")
a2 = pyfits.open("final_processed2.fit")
a3 = pyfits.open("final_processed3.fit")
...

and so on for all of the images in the directory. I want to save them all under a different variable so I don't have to mess around with arrays etc.

Brandon

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
bjd2385
  • 2,013
  • 4
  • 26
  • 47
  • 1
    exec('a%d = pyfits.open("final_processed%d.fit")' % (x, x)). But I will reject code with exec for a public project. – cox Aug 24 '14 at 06:48
  • Why do you think you need this? Just build a dictionary, or even a list of opened files. – Iguananaut Aug 26 '14 at 16:42
  • possible duplicate of [How do you create different variable names while in a loop? (Python)](http://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop-python) – Iguananaut Aug 26 '14 at 16:44

1 Answers1

0

never use exec when you can use something much safer like dictionary, it can be dangerous and is not pythonic!

d={}
for x in range(number_of_your_photos):
        d["a{0}".format(x)]=("final_processed{0}.fit".format(x))

then you have a dictionary of your images!

Mazdak
  • 105,000
  • 18
  • 159
  • 188