Skip to content

Commit b7ad27a

Browse files
authored
Add files via upload
1 parent df3a748 commit b7ad27a

File tree

10 files changed

+2172
-0
lines changed

10 files changed

+2172
-0
lines changed

6-File Handling/6.2-filepath.ipynb

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### Working With File Paths\n",
8+
"When working with files in Python, handling file paths correctly is crucial to ensure your code works across different operating systems and environments. Python provides several modules and functions for working with file paths effectively."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"metadata": {},
15+
"outputs": [
16+
{
17+
"name": "stdout",
18+
"output_type": "stream",
19+
"text": [
20+
"Current working directory is e:\\UDemy Final\\python\\6-File Handling\n"
21+
]
22+
}
23+
],
24+
"source": [
25+
"#### Using the os module\n",
26+
"import os\n",
27+
"cwd=os.getcwd()\n",
28+
"print(f\"Current working directory is {cwd}\")"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 5,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"name": "stdout",
38+
"output_type": "stream",
39+
"text": [
40+
"Directory 'package' create\n"
41+
]
42+
}
43+
],
44+
"source": [
45+
"## create a new directory\n",
46+
"new_directory=\"package\"\n",
47+
"os.mkdir(new_directory)\n",
48+
"print(f\"Directory '{new_directory}' create\")\n"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 6,
54+
"metadata": {},
55+
"outputs": [
56+
{
57+
"name": "stdout",
58+
"output_type": "stream",
59+
"text": [
60+
"['6.1-fileoperation.ipynb', '6.2-filepath.ipynb', 'destination.txt', 'example.bin', 'example.txt', 'package']\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"## Listing Files And Directories\n",
66+
"items=os.listdir('.')\n",
67+
"print(items)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 7,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"folder\\file.txt\n"
80+
]
81+
}
82+
],
83+
"source": [
84+
"### Joining Paths\n",
85+
"\n",
86+
"dir_name=\"folder\"\n",
87+
"file_name=\"file.txt\"\n",
88+
"full_path=os.path.join(dir_name,file_name)\n",
89+
"print(full_path)"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 8,
95+
"metadata": {},
96+
"outputs": [
97+
{
98+
"name": "stdout",
99+
"output_type": "stream",
100+
"text": [
101+
"e:\\UDemy Final\\python\\6-File Handling\\folder\\file.txt\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"\n",
107+
"dir_name=\"folder\"\n",
108+
"file_name=\"file.txt\"\n",
109+
"full_path=os.path.join(os.getcwd(),dir_name,file_name)\n",
110+
"print(full_path)"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 9,
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"name": "stdout",
120+
"output_type": "stream",
121+
"text": [
122+
"The path 'example1.txt' does not exists\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"path='example1.txt'\n",
128+
"if os.path.exists(path):\n",
129+
" print(f\"The path '{path}' exists\")\n",
130+
"else:\n",
131+
" print(f\"The path '{path}' does not exists\")"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 10,
137+
"metadata": {},
138+
"outputs": [
139+
{
140+
"name": "stdout",
141+
"output_type": "stream",
142+
"text": [
143+
"The path 'example.txt' is a file.\n"
144+
]
145+
}
146+
],
147+
"source": [
148+
"#Checking if a Path is a File or Directory\n",
149+
"import os\n",
150+
"\n",
151+
"path = 'example.txt'\n",
152+
"if os.path.isfile(path):\n",
153+
" print(f\"The path '{path}' is a file.\")\n",
154+
"elif os.path.isdir(path):\n",
155+
" print(f\"The path '{path}' is a directory.\")\n",
156+
"else:\n",
157+
" print(f\"The path '{path}' is neither a file nor a directory.\")\n"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 11,
163+
"metadata": {},
164+
"outputs": [
165+
{
166+
"name": "stdout",
167+
"output_type": "stream",
168+
"text": [
169+
"e:\\UDemy Final\\python\\6-File Handling\\example.txt\n"
170+
]
171+
}
172+
],
173+
"source": [
174+
"## Getting the absolute path\n",
175+
"relative_path='example.txt'\n",
176+
"absolute_path=os.path.abspath(relative_path)\n",
177+
"print(absolute_path)"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": null,
183+
"metadata": {},
184+
"outputs": [],
185+
"source": []
186+
}
187+
],
188+
"metadata": {
189+
"kernelspec": {
190+
"display_name": "Python 3",
191+
"language": "python",
192+
"name": "python3"
193+
},
194+
"language_info": {
195+
"codemirror_mode": {
196+
"name": "ipython",
197+
"version": 3
198+
},
199+
"file_extension": ".py",
200+
"mimetype": "text/x-python",
201+
"name": "python",
202+
"nbconvert_exporter": "python",
203+
"pygments_lexer": "ipython3",
204+
"version": "3.12.0"
205+
}
206+
},
207+
"nbformat": 4,
208+
"nbformat_minor": 2
209+
}

0 commit comments

Comments
 (0)