Skip to content

Commit 460c62c

Browse files
authored
Create pathModuleInOS.md
1 parent 662873d commit 460c62c

1 file changed

Lines changed: 350 additions & 0 deletions

File tree

Module/os/pathModuleInOS.md

Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
## os.path module
2+
3+
#### The os.path module from os module
4+
- **os.path** module used for common pathname manipulation.
5+
- This module implements some useful functions on pathnames.
6+
- Different operating systems have different path name conventions.
7+
8+
different path styles:
9+
```
10+
- posixpath for UNIX-style paths
11+
- ntpath for Windows paths
12+
- macpath for old-style MacOS paths
13+
```
14+
#### import os.path module
15+
16+
- Instead of directly importing os.path name module first import os root module and access os.path from it.
17+
```python
18+
#import os module
19+
import os
20+
```
21+
22+
use os.path for further operation
23+
24+
Get documentation of os.path
25+
```python
26+
#import os module
27+
import os
28+
29+
#Get help Documentation of os.path
30+
print(help(os.path))
31+
32+
#Get Avialable Function list in os.path module
33+
print(dir(os.path))
34+
```
35+
36+
Important Function in os.path Module
37+
38+
#### 1.curdir():
39+
- Return a period
40+
- An attribute reference is a primary followed by a period and a name:
41+
42+
attributeref ::= primary "." identifier
43+
44+
primary must evaluate to an object of a type that supports
45+
attribute references, which most objects do. This object is then
46+
asked to produce the attribute whose name is the identifier
47+
48+
Example:
49+
```python
50+
#import os
51+
52+
#get curdir
53+
print(os.path.curdir)
54+
55+
#Result:.
56+
```
57+
58+
#### 2.abspath:
59+
60+
- Return an absolute path.
61+
- Normally it joins using normspath **os.getcwd()** and path and return it.
62+
63+
64+
Syntax:
65+
```python
66+
os.path.abspath(path)
67+
68+
# path paramter is mandatory
69+
```
70+
71+
Example:
72+
```python
73+
import os
74+
75+
#path
76+
path=r"C:/Test"
77+
print(os.path.abspath(path))
78+
79+
#Result :"currentdirectorypath/C:/Test"
80+
```
81+
82+
#### 3.basename:
83+
- Returns the lat component of given path.
84+
85+
Consider following example
86+
```
87+
fullpath:"C:/Test/final"
88+
basename:final
89+
90+
full_path:"C:/Test/final/"
91+
basename:
92+
93+
full_path:"C:/Test/final/filename.txt"
94+
basename:filename.txt
95+
```
96+
97+
Syntax:
98+
```python
99+
os.path.basename(path)
100+
```
101+
102+
Example:
103+
```
104+
#Import Required module
105+
import os
106+
107+
#Full path
108+
full_path=r"C:/Test/final/filename.txt"
109+
110+
#Get basename
111+
basename=os.path.basename(full_path)
112+
113+
print(basename)
114+
#Result:filename.txt
115+
```
116+
117+
#### 4.commonpath
118+
119+
- From 2 given path, returns the longest common sub-path.
120+
Consider
121+
```
122+
path1="C:/test/folder/amaze"
123+
path2="C:/test/folder"
124+
125+
commonpath:C:/test/folder
126+
```
127+
128+
Syntax:
129+
```python
130+
os.path.commonpath(['path1','path2','path3'])
131+
```
132+
133+
Example:
134+
```python
135+
136+
# import require dmodule
137+
import os
138+
139+
#path list
140+
paths=['/usr/lib', '/usr/local/lib']
141+
142+
#To get Common Path
143+
common=os.path.commonpath(paths)
144+
145+
#print the commonpath
146+
print(common)
147+
#Result:/usr
148+
```
149+
150+
#### 5.commonprefix()
151+
152+
- From a list of pathnames, returns the longest common leading path.
153+
154+
Consider following Example
155+
```
156+
path1="C:/test/folder/amaze"
157+
path2="C:/test/fntl/test"
158+
159+
commonprefix path:C:/test/f
160+
```
161+
162+
Syntax:
163+
```python
164+
os.path.commonprefix(['path1','path2'])
165+
```
166+
167+
Example:
168+
```python
169+
# import require dmodule
170+
import os
171+
172+
#Initialize paths
173+
path1="C:/test/folder/amaze"
174+
path2="C:/test/fntl/test"
175+
176+
#get common prefix in path
177+
comonprefix=os.path.commonprefix([path1,path2])
178+
179+
#print Common prefix path
180+
print(comonprefix)
181+
182+
#Result:C:/test/f
183+
```
184+
185+
186+
#### 6.dirname:
187+
- Return the directory path of given pathname
188+
189+
Consider
190+
```
191+
pathname="C:/test/folder/amaze.txt"
192+
dirname:C:/test/folder
193+
```
194+
195+
Syntax:
196+
```python
197+
os.path.dirname('path')
198+
```
199+
200+
Example:
201+
```python
202+
# import require dmodule
203+
import os
204+
205+
#Initialize paths
206+
path1="C:/test/folder/amaze.txt"
207+
208+
#Get dir name
209+
name=os.path.dirname(path1)
210+
211+
212+
#print the dirname
213+
print(name)
214+
215+
#Result:C:/test/folder
216+
```
217+
218+
219+
#### 7.exists
220+
221+
- Return True if given pathname is exists otherwise return False
222+
223+
Syntax:
224+
```
225+
os.path.exists('pathname')
226+
```
227+
228+
Example:
229+
```python
230+
import os
231+
path=r"C:/test/folder/amaze.txt"
232+
print(os.path.exists(path))
233+
234+
#Result:False because this path is not exists
235+
```
236+
237+
#### 8.isfile
238+
239+
- Return True if given pathname is a file
240+
241+
Syntax:
242+
```python
243+
os.path.isfile(path)
244+
```
245+
246+
Example:
247+
```python
248+
import os
249+
path=r"C:/test/folder/amaze.txt"
250+
print(os.path.isfile(path))
251+
252+
#Result is True if that file exists and it is a a file otherwise False
253+
```
254+
255+
#### 9.isdir
256+
257+
- Return True if given pathname is a directory
258+
259+
Syntax:
260+
```python
261+
os.path.isdir(path)
262+
```
263+
264+
Example:
265+
```python
266+
import os
267+
path=r"C:/test/folder"
268+
print(os.path.isdir(path))
269+
270+
#Result is True if that directory is exists and it is a a directory otherwise False
271+
```
272+
273+
#### 10.join:
274+
- join two or more pathname paths, by inserting **'/'** as needed.
275+
276+
Syntax:
277+
```python
278+
os.path.join(path1,path2)
279+
```
280+
281+
Example:
282+
```python
283+
#import required module
284+
import os
285+
286+
path1=r"c:"
287+
path2=r"foldername"
288+
289+
#join path1 and path2
290+
joined_path=os.path.join(path1,path2)
291+
292+
print(joined_path)
293+
#Result:c:/foldername
294+
```
295+
296+
#### 11.split:
297+
- split(path) Return a tuple containing
298+
(head,tail)
299+
300+
**tail**:where tail is everything after the final slash may be empty sometime
301+
302+
- The tail part will never contain a slash.
303+
- If path is empty, both head and tail are empty.
304+
305+
Syntax:
306+
```python
307+
os.path.split()
308+
```
309+
310+
Example:
311+
```python
312+
#import required module
313+
import os
314+
315+
#Simple Example 1
316+
path1=r"C:/Test/help.txt"
317+
splited_path1=os.path.split(path1)
318+
print(splited_path1)
319+
#Result:('C:/Test', 'help.txt')
320+
321+
#Example 2
322+
path2=r"C:/Test/"
323+
splited_path2=os.path.split(path2)
324+
print(splited_path2)
325+
#Result:('C:/Test', '')
326+
327+
#Example 3
328+
path3=r""
329+
splited_path3=os.path.split(path3)
330+
print(splited_path3)
331+
#Result:('', '')
332+
333+
'''
334+
#Result:
335+
('C:/Test', 'help.txt')
336+
('C:/Test', '')
337+
('', '')
338+
'''
339+
```
340+
341+
Some Other Important os.path avialable Function
342+
343+
**12.splitdrive:**
344+
- Split a pathname into drive and path,
345+
346+
**13.splittext**
347+
- Split the extension from a pathname.
348+
349+
**14.relpath(path, start=os.curdir)**
350+
- Return a relative filepath to path either from the current directory or from an optional start directory

0 commit comments

Comments
 (0)