Inducción al uso de Python

Práctica 8: Archivos

Objective

During this activity, students should be able to:


Activity Description

Solve the following set of problems using Python 3. Run and test each of your programs to make sure they work as expected.

  1. Write a program called wordcount.py. Define in this program a function called wordcount(infile) that takes a string with the name of an input file infile and returns a tuple with the number of lines, number of words, and number of characters in the file.

    To test your program, create a file called crocodile.txt with the following content:

    HOW DOTH THE LITTLE CROCODILE
    Lewis Carroll
    
    How doth the little crocodile
    Improve his shining tail,
    And pour the waters of the Nile
    On every golden scale!
    
    How cheerfully he seems to grin,
    How neatly spread his claws,
    And welcome little fishes in
    With gently smiling jaws!
    

    Add the following main() function to your program:

    def main():
        print(wordcount('crocodile.txt'))
    

    The expected output after running the program should be:

    (12, 47, 274)

    This is because the file crocodile.txt has 12 lines, 47 words, and 274 characters. NOTE: These numbers may vary slightly depending of the operating system being used.

  2. Write a program called addints.py. Define in this program a function called addints(infile) that takes one argument: a string with the name of the input file infile that contains a sequence of integer numbers, each number in its own line. The function should return the result of adding all these integers together.

    To test your program, create a file called numbers.txt with the following content:

    4
    8
    15
    16
    23
    42

    Add the following main() function to your program:

    def main():
        print(addints('numbers.txt'))
    

    The expected output after running the program should be:

    108
  3. Write a program called reverse.py. Define in this program a function called reverse(infile, outfile) that takes two arguments: a string with the name of the input file infile and a string with the name of the output file outfile. The function should write in outfile each of the lines contained in infile but in reversed order. The function should return nothing.

    To test your program, create a file called speak.txt with the following content:

    SPEAK ROUGHLY
    Lewis Carroll
    
    Speak roughly to your little boy,
    And beat him when he sneezes:
    He only does it to annoy,
    Because he knows it teases.
    
    Chorus
    Wow! wow! wow!
    
    I speak severely to my boy,
    I beat him when he sneezes;
    For he can thoroughly enjoy
    The pepper when he pleases!
    

    Add the following main() function to your program:

    def main():
        reverse('speak.txt', 'speak_reverse.txt')
    

    After running your program, the contents of the file speak_reverse.txt should be:

    The pepper when he pleases!
    For he can thoroughly enjoy
    I beat him when he sneezes;
    I speak severely to my boy,
    
    Wow! wow! wow!
    Chorus
    
    Because he knows it teases.
    He only does it to annoy,
    And beat him when he sneezes:
    Speak roughly to your little boy,
    
    Lewis Carroll
    SPEAK ROUGHLY
    
  4. Write a program called countv.py. Define in this program a function called countv(infile) that takes a string with the name of an input file infile and returns the number of vowels contained in the specified file.

    To test your program, create a file called hanging.txt with the following content:

    THE HANGING TREE
    Suzanne Collins
    
    Are you, are you
    Coming to the tree
    Where they strung up a man they say murdered three.

    Add the following main() function to your program:

    def main():
        print(countv('hanging.txt'))
    

    The expected output after running the program should be:

    38
  5. Write a program called words.py. Define in this program a function called words(outfile, s) that takes two strings: the name of an output file outfile and a string phrase s. The function should create an new file with the given name containing each word of the phrase s in its own line.

    Use the following main() function to test your program:

    def main():
        words("output.txt",
              "I'm gonna make him an offer he can't refuse")
    

    After running your program, the contents of the file output.txt should be:

    I'm
    gonna
    make
    him
    an
    offer
    he
    can't
    refuse
  6. Write a program called linenum.py. Define in this program a function called linenum(infile, outfile) that takes two arguments: a string with the name of the input file infile and a string with the name of the output file outfile. The function should write in outfile each of the lines contained in infile but adding the line number along the left edge. The function should return nothing.

    To test your program, create a file called bat.txt with the following content:

    THE BAT
    Lewis Carroll
    
    Twinkle, twinkle, little bat
    How I wonder what you're at!
    Up above the world you fly
    Like a tea-tray in the sky.
    

    Add the following main() function to your program:

    def main():
        linenum('bat.txt', 'bat_linenum.txt')
    

    After running your program, the contents of the file bat_linenum.txt should be:

    1: THE BAT
    2: Lewis Carroll
    3:
    4: Twinkle, twinkle, little bat
    5: How I wonder what you're at!
    6: Up above the world you fly
    7: Like a tea-tray in the sky.