Creating a new file with input file name
<p>I have a question. How can I rename the output file using the input filename?</p>

<p>For example, my input filename is: </p>

<pre><code>Field_52_combined_final_roughcal.fits
</code></pre>

<p>I would like to obtain an output filename like: </p>

<pre><code>Field_52_traitement_1.fits
</code></pre>

<p>I know that I could write : </p>

<pre><code>hdu.writeto('Field_52_traitement_1.fits')   
</code></pre>

<p>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.</p>

<p>My script looks like this (for a single input file):</p>

<pre><code>#!/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'))    
</code></pre>

<p>But, with this kind of script, I get:</p>

<pre><code>Field_52_combined_final_roughcal.fits_traitement_1
</code></pre>

<p>Tell me, if you have any ideas, websites or something else :)
Thank you for your answers!</p>