Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions file-io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -70,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -99,7 +99,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -108,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -119,20 +119,9 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<generator object Path.glob at 0x71ead4273450>"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from pathlib import Path\n",
"\n",
Expand All @@ -146,7 +135,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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": {
Expand Down
16 changes: 16 additions & 0 deletions my_files/all_lines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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