Search code examples
pythonfilemoveshutilfits

moving multiple files by a parameter applied beforehand


I have problem moving a dependent file. I have lots of files with 2 different exts. For example ; xenian10.fits, xenian11.fits, xenian12.fits and for each there is an equilevant version with different ext like xenian10.no, xenian11.no, xenian12.no and so on.

All the .fits files contain a different parameter so it's something like this;

import os
import glob
import shutil
import pyfits

for fitsName in glob.glob('*.fits'):
    hdulist = pyfits.open(fitsName)
    hdu = hdulist[0]
    a= hdulist[0].header['ITEM']
    if a == "color":
        shutil.move(fitsName, '/home/color/')
    b = os.path.splitext(fitsName)[O] + '.no'
    shutil.move(b, '/home/color/')
    if a == "smell":
        shutil.move(fitsName, '/home/smell/')
    b = os.path.splitext(fitsName)[O] + '.no'
    shutil.move(b, '/home/smell/')

The problem here is for the first line that contains the splitex;

b = os.path.splitex(fitsName)[O] + '.no'
shutil.move(b, '/home/color/')

moves all the files with .no ext files. However, I want it to be only linked to the .fits files that were moved to by the"color" parameter from the previous line of the code only. Because currently if only xenian10.fits is with the parameter "color" among all others, then only the corresponding .fits file moves, so that part is ok, but after that I want only xenian10.no file to move, however the code moves all of the .no files inside folder disregarding that the only .fits moved was xenian10.fits.


Solution

  • Problem was only with indentation.

    import os
    import glob
    import shutil
    import pyfits
    
    for fitsName in glob.glob('*.fits'):
        hdulist = pyfits.open(fitsName)
        hdu = hdulist[0]
        a= hdulist[0].header['ITEM']
        if a == "color":
            shutil.move(fitsName, '/home/color/')
            b = os.path.splitext(fitsName)[O] + '.no'
            shutil.move(b, '/home/color/')
        if a == "smell":
            shutil.move(fitsName, '/home/smell/')
            b = os.path.splitext(fitsName)[O] + '.no'
            shutil.move(b, '/home/smell/')