{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with files and folders - Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 7.1.\n", "\n", "Download the following file:\n", "\n", "[edu.nl/ggwtf](edu.nl/ggwtf)\n", "\n", "The file contains the text of Sonnet 116 by William Shakespeare. Write code which can generate line numbers. The output should look as follows:\n", "\n", "\n", "```\n", "1. Let me not to the marriage of true minds\n", "2. Admit impediments. Love is not love\n", "3. Which alters when it alteration finds,\n", "4. Or bends with the remover to remove:\n", "[…]\n", "```\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.\tLet me not to the marriage of true minds\n", "2.\tAdmit impediments. Love is not love\n", "3.\tWhich alters when it alteration finds,\n", "4.\tOr bends with the remover to remove:\n", "5.\tO, no! it is an ever-fixed mark,\n", "6.\tThat looks on tempests and is never shaken;\n", "7.\tIt is the star to every wandering bark,\n", "8.\tWhose worth’s unknown, although his height be taken.\n", "9.\tLove’s not Time’s fool, though rosy lips and cheeks\n", "10.\tWithin his bending sickle’s compass come;\n", "11.\tLove alters not with his brief hours and weeks,\n", "12.\tBut bears it out even to the edge of doom.\n", "13.\tIf this be error and upon me proved,\n", "14.\tI never writ, nor no man ever loved.\n" ] } ], "source": [ "sonnet = open( '../Texts/sonnet116.txt' , encoding = 'utf-8' )\n", "\n", "lineNr = 0\n", "\n", "\n", "for line in sonnet:\n", " lineNr += 1\n", " print('{}.\\t{}'.format( lineNr , line.strip() ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 7.2.\n", "\n", "Using the file containing the text of Sonnet 116, create a new file named\n", "‘data.txt’, which gives information about the following:\n", "\n", "* The total number of characters in the file\n", "* The number of lines\n", "* The average number of characters per line\n", "* The longest line of the sonnet (i.e. the line with the highest number of\n", "characters)\n", "\n", "The average number of characters per line can be calculated by dividing the\n", "total number of characters by the number of lines." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sonnet = open( 'sonnet116.txt' )\n", "out = open( 'data.txt' , 'w' )\n", "\n", "totalNrCharacters = 0\n", "lineNr = 0\n", "## variable max is the line with the highest number of characters\n", "max = ''\n", "\n", "\n", "for line in sonnet:\n", " totalNrCharacters += len(line)\n", " lineNr += 1\n", " if len(line) > len(max):\n", " max = line.strip()\n", "\n", "out.write( 'Total number of characters: {}\\n'.format( totalNrCharacters ) )\n", "out.write( 'Number of lines: {}\\n'.format( lineNr ) )\n", "out.write( 'Average number of characters per line: {}\\n'.format( totalNrCharacters / lineNr ) )\n", "out.write( \"Longest line: '{}'\\n\".format( max ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 7.3.\n", "\n", "Using the ‘listdir()’ function from the os library, write code which can list all the files in a given folder on your own computer." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from os.path import join\n", "\n", "dir = '/Users/verhaarpaf/Documents'\n", "\n", "for file in os.listdir(dir):\n", " print( join( dir , file ) )" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }