OS Path module in Python

Python Methods and Functions

Below are some of the functions of the OS Path module.

1. os.path.basename (path): is used to return the base name of the file. This function basically returns the filename at the specified path.

# basename function

import os

out = os.path.basename ( "/ baz / foo" )

print (out)

Output:

 'foo' 

2. os.path.dirname (path): is used to return the name of the directory at the specified path. This function returns the name from the path, except for the path name.

# dirname function

import os

out = os.path.basename ( "/ baz / foo" )

print (out)

Output:

 '/ baz' 

3. os.path.isabs (path): indicates whether the path is absolute or not. On Unix, an absolute path means the path starts with a forward slash (& # 39; / & # 39;), while on Windows it starts with a (back) forward slash after cutting off a potential drive letter.

# isabs function

import os

out = os.path.isabs ( "/ baz / foo" )

print (out)

Exit:

 True 

4. os.path.isdir (path): this function indicates whether the path is an existing directory or not.

# isdir function

import os

out = os.path.isdir ( "C: Users" )

print (out)

Output:

 True 

5. os.path.normcase (path): This function normalizes the case of the specified path. On Unix and Mac OS X systems, it returns the path as is. But on Windows it converts the path to lowercase and forward slash to backslash.

# normal function in windows

import os

out = os.path.normcase ( "/ BAz" )

print (out)

Output:

 ' baz' 

6. os.path.normpath (path): This function normalizes pathnames by collapsing excess top level delimiters and references to A // B, A / B /, A / ./ B and A / foo / .. / B all become A / B. On Windows, it converts forward slashes to backslashes.

Unix normpath function

import os

out = os.path .normpath ( "foo /./ bar" )

print (out)

Output:

 'foo / bar' 

There are many more functions, you can link to this in Python .

Links:
Python Documentation





Tutorials