From be2601173a6f5c5ba17ef7a8667315309ed7a93f Mon Sep 17 00:00:00 2001 From: connornight7 <157524386+connornight7@users.noreply.github.com> Date: Sun, 23 Feb 2025 23:54:42 +0000 Subject: [PATCH] FILE I/O HW --- file-io.ipynb | 70 +++++++++++++++++++++++++++++------------- my_files/all_lines.txt | 16 ++++++++++ 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/file-io.ipynb b/file-io.ipynb index 5560737..08eab3f 100644 --- a/file-io.ipynb +++ b/file-io.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -99,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -108,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -119,20 +119,9 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from pathlib import Path\n", "\n", @@ -146,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -155,6 +144,45 @@ " with open(iliad_file, \"r\") as f:\n", " a.write(f.read() + \"\\n\")\n" ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Successfully split the file into 27 parts.\n" + ] + } + ], + "source": [ + "#Homework part\n", + "from pathlib import Path\n", + "\n", + "# Define file and output directory\n", + "input_file = \"pope-iliad.txt\"\n", + "output_dir = Path(\"sliced_texts\")\n", + "output_dir.mkdir(exist_ok=True) # Create directory if it doesn't exist\n", + "\n", + "# Read the file\n", + "with open(input_file, encoding=\"utf-8\") as f:\n", + " lines = f.readlines() # Read all lines\n", + "\n", + "# Choose a slice of 1000 lines (adjust slice range as needed)\n", + "slice_size = 1000\n", + "for i in range(0, len(lines), slice_size):\n", + " chunk = lines[i:i + slice_size] # Get 1000-line slice\n", + " output_file = output_dir / f\"iliad_part_{i // slice_size + 1}.txt\"\n", + " \n", + " # Write the chunk to a new file\n", + " with open(output_file, \"w\", encoding=\"utf-8\") as f:\n", + " f.writelines(chunk)\n", + "\n", + "print(f\"Successfully split the file into {len(lines) // slice_size + 1} parts.\")\n" + ] } ], "metadata": { diff --git a/my_files/all_lines.txt b/my_files/all_lines.txt index 4b9ed27..559ed8d 100644 --- a/my_files/all_lines.txt +++ b/my_files/all_lines.txt @@ -22,3 +22,19 @@ With tame content, and thou possess’d of thine? Great as thou art, and like a god in fight, Such as a king might ask; and let it be Then thus the king: “Shall I my prize resign +At thy demand shall I restore the maid? +A treasure worthy her, and worthy me. +First let the just equivalent be paid; +Think not to rob me of a soldier’s right. +With tame content, and thou possess’d of thine? +Great as thou art, and like a god in fight, +Such as a king might ask; and let it be +Then thus the king: “Shall I my prize resign +At thy demand shall I restore the maid? +A treasure worthy her, and worthy me. +First let the just equivalent be paid; +Think not to rob me of a soldier’s right. +With tame content, and thou possess’d of thine? +Great as thou art, and like a god in fight, +Such as a king might ask; and let it be +Then thus the king: “Shall I my prize resign