如何在python中保存多个不同文件名的文件?
How to save multiple files with different file names in python?
我想用不同的文件名保存每个文件。我知道如何对单个文件执行如下操作:
nib.Nifti1Image(wt, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode()+"_unc_whole.nii.gz"))
nib.Nifti1Image(tc, affine).to_filename(os.path.join(output_dir,data_file.root.subject_ids[data_index].decode()+ "_unc_core.nii.gz"))
nib.Nifti1Image(et, affine).to_filename(os.path.join(output_dir,data_file.root.subject_ids[data_index].decode()+ "_unc_enhance.nii.gz"))
nib.Nifti1Image(alea, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode()+"_alea.nii.gz"))
如何使用 for 循环执行此操作,这样我就不必为每个单独的文件重复该函数?
作为一个简单的解决方案,您可以使用 zip
:
alist = [wt, tc, et, alea]
blist = ["_unc_whole.nii.gz", "_unc_core.nii.gz", "_unc_enhance.nii.gz", "_alea.nii.gz"]
for a, b in zip(alist, blist):
nib.Nifti1Image(a, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode() + b))
我想用不同的文件名保存每个文件。我知道如何对单个文件执行如下操作:
nib.Nifti1Image(wt, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode()+"_unc_whole.nii.gz"))
nib.Nifti1Image(tc, affine).to_filename(os.path.join(output_dir,data_file.root.subject_ids[data_index].decode()+ "_unc_core.nii.gz"))
nib.Nifti1Image(et, affine).to_filename(os.path.join(output_dir,data_file.root.subject_ids[data_index].decode()+ "_unc_enhance.nii.gz"))
nib.Nifti1Image(alea, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode()+"_alea.nii.gz"))
如何使用 for 循环执行此操作,这样我就不必为每个单独的文件重复该函数?
作为一个简单的解决方案,您可以使用 zip
:
alist = [wt, tc, et, alea]
blist = ["_unc_whole.nii.gz", "_unc_core.nii.gz", "_unc_enhance.nii.gz", "_alea.nii.gz"]
for a, b in zip(alist, blist):
nib.Nifti1Image(a, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode() + b))