-5

I'm building a function that needs to operate over several files, in the following way:

def myfuntion(filenames):
    n = len(filenames)
    if n > 0:
        data = fits.open(filenames)
        for i in range(1,n):
            final = *DO SOMETHING FOR EACH FILE*
    return final 

The idea is to call the function using several files:

mean_myfuntion(['file1.fits', 'file2.fits', 'file3.fits'])

However the problem is that I'm receiving the following error:

OSError: File-like object does not have a 'write' method, required for mode 'ostream'.

And I'm not sure what's going on. I'm suspecting that I'm missing something when calling the filenames (data = fits.open(filenames)), If you folks have any advise, it will be more than welcome.

Thanks!

FMEZA
  • 51
  • 1
  • 9
  • 1
    can you add the `DO SOMETHING FOR EACH FILE` part ? – Chiheb Nexus Aug 03 '17 at 04:04
  • Where does in [its documentation](http://astropy.readthedocs.io/en/latest/io/fits/api/files.html) says that it can work with list of filenames? It clearly expects a single file name at once hence you need to loop over filenames and pass one at a time. – Ashwini Chaudhary Aug 03 '17 at 04:06
  • Check out this thread: https://stackoverflow.com/questions/29703939/python-pyfits-cannot-open-file – Peter Wang Aug 03 '17 at 04:07

2 Answers2

1

The problem with your code is, you are giving a list as an arugument to fits.open() but the str is required: see Docs. So you need to loop through the list to get file name one by one as 'str'.

def myfuntion(filenames):
    n = len(filenames)
    if n > 0:
        for file in filenames:
            data = fits.open(file)
            final = *DO SOMETHING FOR EACH FILE*
    return final 
Rahul
  • 10,830
  • 4
  • 53
  • 88
-1

try with open like below:

with open(file, 'r') as f:
    # *DO SOMETHING FOR EACH FILE*
bfontaine
  • 18,169
  • 13
  • 73
  • 107
Jape
  • 11
  • 5