Last active
August 25, 2020 19:05
-
-
Save ivangeorgiev/12d541d4a0fa1aa926be59e4411b5009 to your computer and use it in GitHub Desktop.
CountLinesInMultipleFiles.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "CountLinesInMultipleFiles.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyOEsE7HPLnkXt642gB2e7D+", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ivangeorgiev/12d541d4a0fa1aa926be59e4411b5009/CountLinesInMultipleFiles.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pXpAE07JoEfZ", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "9117e593-6e2f-4b36-abf9-ff56a741a7a4" | |
}, | |
"source": [ | |
"import glob\n", | |
"import itertools\n", | |
"\n", | |
"def open_files(filenames):\n", | |
" for filename in filenames:\n", | |
" with open(filename) as file:\n", | |
" yield file\n", | |
"\n", | |
"filenames = glob.glob('**/*')\n", | |
"files = open_files(filenames)\n", | |
"lines = itertools.chain.from_iterable(files)\n", | |
"ones_per_line = map(lambda line: 1, lines)\n", | |
"line_count = sum(ones_per_line)\n", | |
"\n", | |
"print(line_count)" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"50070\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8EsgddNqonBe", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 1, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment