| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
different path styles:
- posixpath for UNIX-style paths - ntpath for Windows paths - macpath for old-style MacOS paths
#import os module
import osuse os.path for further operation
Get documentation of os.path
#import os module
import os
#Get help Documentation of os.path
print(help(os.path))
#Get Avialable Function list in os.path module
print(dir(os.path))Important Function in os.path Module
Return a period
An attribute reference is a primary followed by a period and a name:
attributeref ::= primary "." identifier
primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier
Example:
#import os
#get curdir
print(os.path.curdir)Output:
.
Syntax:
os.path.abspath(path)
# path parameter is mandatoryExample:
import os
#path
path=r"C:/Test"
print(os.path.abspath(path))Output:
currentdirectorypath/C:/Test
Consider following example
fullpath:"C:/Test/final" basename:final full_path:"C:/Test/final/" basename: full_path:"C:/Test/final/filename.txt" basename:filename.txt
Syntax:
os.path.basename(path)Example:
#Import Required module import os #Full path full_path=r"C:/Test/final/filename.txt" #Get basename basename=os.path.basename(full_path) print(basename)
Output:
filename.txt
path1="C:/test/folder/amaze" path2="C:/test/folder" commonpath:C:/test/folder
Syntax:
os.path.commonpath(['path1','path2','path3'])Example:
# import require module
import os
#path list
paths=['/usr/lib', '/usr/local/lib']
#To get Common Path
common=os.path.commonpath(paths)
#print the commonpath
print(common)Ouput:
/usr
Consider following Example
path1="C:/test/folder/amaze" path2="C:/test/fntl/test" commonprefix path:C:/test/f
Syntax:
os.path.commonprefix(['path1','path2'])Example:
# import require dmodule
import os
#Initialize paths
path1="C:/test/folder/amaze"
path2="C:/test/fntl/test"
#get common prefix in path
comonprefix=os.path.commonprefix([path1,path2])
#print Common prefix path
print(comonprefix)OutPut:
C:/test/f
Consider
pathname="C:/test/folder/amaze.txt" dirname:C:/test/folder
Syntax:
os.path.dirname('path')Example:
# import require dmodule
import os
#Initialize paths
path1="C:/test/folder/amaze.txt"
#Get dir name
name=os.path.dirname(path1)
#print the dirname
print(name)OutPut:
C:/test/folder
Syntax:
os.path.exists('pathname')
Example:
import os
path=r"C:/test/folder/amaze.txt"
print(os.path.exists(path))Output:
False
Syntax:
os.path.isfile(path)Example:
import os
path=r"C:/test/folder/amaze.txt"
print(os.path.isfile(path))
#Result is True if that file exists and it is a file otherwise FalseOutput:
True
Syntax:
os.path.isdir(path)Example:
import os
path=r"C:/test/folder"
print(os.path.isdir(path))
#Result is True if that directory exists and it is a directory otherwise FalseOutput:
True
Syntax:
os.path.join(path1,path2)Example:
#import required module
import os
path1=r"c:"
path2=r"foldername"
#join path1 and path2
joined_path=os.path.join(path1,path2)
print(joined_path)Output:
c:/foldername
tail: where the tail is everything after the final slash may be empty sometime
Syntax:
os.path.split()Example:
#import required module
import os
#Simple Example 1
path1=r"C:/Test/help.txt"
splited_path1=os.path.split(path1)
print(splited_path1)
#Result:('C:/Test', 'help.txt')
#Example 2
path2=r"C:/Test/"
splited_path2=os.path.split(path2)
print(splited_path2)
#Result:('C:/Test', '')
#Example 3
path3=r""
splited_path3=os.path.split(path3)
print(splited_path3)
#Result:('', '')Output:
('C:/Test', 'help.txt')
('C:/Test', '')
('', '')
Some Other Important os.path available Function
12.splitdrive:
13.splittext
14.relpath(path, start=os.curdir)
| Back | FazBrowse Home | New Git URL |