perl2python: Reading files
Table of Contents File Read files HERE documents File Read files • perl #!/usr/bin/perl -w use strict ; open FILE, " data/test.fa " or die " $! " ; while ( my $line = <FILE> ){ print " $line " ; } ## >gene1 ## ATGGCTAGCATCGTACGTCG ## >gene2 ## ATGGCTAGCTACGTACGTAG • python infile = open ( "data/test.fa" , 'r' ) for line in infile.readlines(): line = line.rstrip( ' \n ' ) print line ## >gene1 ## ATGGCTAGCATCGTACGTCG ## >gene2 ## ATGGCTAGCTACGTACGTAG Here readline() reads one line character at a time, readlines() reads in the whole file at once and splits it by line. The xreadlines() function should be used for big files: infile = open ( "data/test.fa" , 'r' ) for line in infile.xreadlines(): line = line.rstrip( ' \n ' ) print line ## >gene1 ## ATGGCTAGC