Skip to content

Commit

Permalink
handling files
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadelmalah committed Apr 20, 2024
1 parent 017db36 commit 4d64739
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is the first line
This is the second line
This is the third line
117 changes: 117 additions & 0 deletions handling_files.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "385e02b5-3594-4a95-bc38-c83c196b5ccb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the first line\n",
"\n",
"This is the second line\n",
"\n",
"This is the third line\n"
]
}
],
"source": [
"with open('example.txt') as file:\n",
" for line in file:\n",
" print(line)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fde4822a-a9fc-42b8-b6aa-e821d596fcad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the first line\n",
"This is the second line\n",
"This is the third line\n"
]
}
],
"source": [
"with open('example.txt') as file:\n",
" print(file.read())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6017aa72-b734-45d9-8a4e-5f7721c9e61a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is th\n"
]
}
],
"source": [
"with open('example.txt') as file:\n",
" print(file.read(10))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3c910f18-e13a-456e-9ceb-841cecb2a5c8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yes, it exists\n"
]
}
],
"source": [
"import os\n",
"if os.path.exists(\"example.txt\"):\n",
" print(\"Yes, it exists\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83d65bb9-8569-4de3-8b8a-892ddb4d10d0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 4d64739

Please sign in to comment.