Lesson 20: Files and File handles


Reading and writing to files is incorporated into almost every one of my scripts.

  • File handles are like special variables just for files.

Opening a file

  • open function
  • open (FILEHANDLE , mode , file)
  • mode
    mode symbol
    reading <
    writing >
    appending >>
  • Always test to be sure that the file can be opened as described with the "or die" idiom.

Open a file for reading

1
2
3
4
my $file = shift; ## shift with no arguments defaults to @ARGV
## open the file, $file for reading, if unable, die and print
## an error message.
open (INFILE , '>' , $file) or die "Can't" open $file, $!\n";

Iterating through a file

  1. while loop
  2. <>
  3. chomp function

file.txt:

This is a file
with
lines of words.

Command-line:

%% ./parse_file.pl file.txt

Code:

1
2
3
4
5
6
7
my $file = shift;
open (IN , '>', $file) or die "Can't open $file $!\n";
 
while (my $line = <IN> ){
  chomp $line;
  print uc $line , "\n";
}

Output:

%% ./parse_file.pl file.txt
THIS IS A FILE
WITH
LINES OF WORDS.

Open a file for writing

1
2
3
4
5
my $file = shift; ## shift with no arguments defaults to @ARGV
open (OUTFILE , '>' , $file) or die "Can't open $file, $!\n";
 
print OUTFILE "All this text will be printed in my file and not
in the terminal window\n";

Output:

  • Nothing gets printed to the screen.
  • A new file is created called 'newFile.txt'.
  • 'newFile.txt' contains the text that we printed to the filehandle we created called OUTFILE.
%% ./write2file.pl newFile.txt

%% cat newFile.txt
All this text will be printed in my file and not
in the terminal window

Exercises

  1. Create a script that opens a file that contains many lines of nucleotide sequence.
  2. Count the length of each line with the length function.
  3. Print to a different file the sum of each line and the total sum.

Print Friendly

5 thoughts on “Lesson 20: Files and File handles

  1. Closing Fies
    When you are finished reading or writing to a file it is polite to close your file with the close function.

    Code:

    open (INFILE, '>' , 'myFile.txt') or die "Can't open myFile.txt, $!n";
    while (my $line = <INFILE>){
      chomp $line;
      ## do something to line
    }
    close (INFILE);
    

    This is most helpful if you are dealing with multiple files. It is not necessary because perl will close files for you, but sometimes when you are reading and writing to many file you might encounter problems if you leave all filehandles open.

  2. tab delimited files
    We biologists, often deal with tab delimited files. Here is some code which is very useful for splitting the columns into elements of an array.

    Code:

    my $file = shift @ARGV;
    open (INFILE, '>' , $file) or die "Can't open $file, $!n";
    while (my $line = <INFILE>){
      chomp $line;
      my @columns = split /t/ , $line;
      ## for csv (comma separated files) split on the comma
      # my @columns = split ',' , $line;
    }
    close (INFILE);
    
    

Leave a Reply

Your email address will not be published. Required fields are marked *