I have a question. How can I rename the output file using the input filename?
For example, my input filename is:
Field_52_combined_final_roughcal.fits
I would like to obtain an output filename like:
Field_52_traitement_1.fits
I know that I could write :
hdu.writeto('Field_52_traitement_1.fits')
But, I have an other script which loop on 200 files, and I would like the output filename being automatically generated by the input filename.
My script looks like this (for a single input file):
#!/usr/bin/python
# coding: utf-8
from astropy.io import fits
from astropy.table import Table
import numpy as np
###################################
# Fichier contenant le champ brut #
###################################
filename = 'E:/Fields/Field52_combined_final_roughcal.fits'
# Ouverture du fichier à l'aide d'astropy
field = fits.open(filename)
# Lecture des données fits
tbdata = field[1].data
#######################################################
# Application du tri en fonction de divers paramètres #
#######################################################
Several conditions / sort
###################################################
# Ecriture du résultat dans nouveau fichier .fits #
###################################################
hdu = fits.BinTableHDU(data=tbdata_final)
hdu.writeto('{}_{}'.format(filename,'traitement_1'))
But, with this kind of script, I get:
Field_52_combined_final_roughcal.fits_traitement_1
Tell me, if you have any ideas, websites or something else :) Thank you for your answers!
os.pathfunctions to separate the filename from the extension or, since you seem to consistently use underscores, simply do afilename.split("_")to get a list of all the parts of the filename. You can then piece it back together in a different order via the list indices. index 0 would be "Field", index 1 would be "52" and so on. You can then "_".join()` afterwards to create a new filename