foreach $id (keys %id2seq){ if (-f $id) { print $id." already exists. about to overwrite it\n";} open my $out_fh, '>', $id or die $!; ##Amendment here print $out_fh ($id."\n",$id2seq{$id}, "\n"); close $out_fh; ## moved into the foreach loop }