my $file = 'C:/ppp.txt'; print "file '$file' "; if (-e $file) { print 'exists '; if (-z $file) { # print 'but is empty'; my $fh, '>>', $file, or die "Could not open file '$file' for appending: $!"; print$fh $address . ' or whatever you want'; close $fh; } else { # print 'and is not empty'; ... (your "old" open and while loop) } } else { # print q!doesn't exist!; ... (either create it or print error message) } print "\n";