Menu Close

OS operating system module using path parameter

This module implements some useful functions on pathnames. The path parameters can be passed as either strings, or bytes. Applications are encouraged to represent file names as (Unicode) character strings.

Join Paths

To join two or more path components together, firstly import os module of python and then use following:

import os
temp = os.path.join('a', 'b', 'c')
print(temp)

# OUTPUT:  a/b/c

The advantage of using os.path is that it allows code to remain compatible over all operating systems, as this uses the separator appropriate for the platform it’s running on.

Path Component Manipulation

To split one component off of the path:

>>> import os 
>>> temp = os.path.join(os.getcwd(), 'FcuktheCode.txt')
>>> temp
'/home/legacyboy/Desktop/FcuktheCode.txt'
>>> os.path.dirname(temp)
'/home/legacyboy/Desktop'
>>> os.path.basename(temp)
'FcuktheCode.txt' 
>>> os.path.split(os.getcwd())
('/home/legacyboy', 'Desktop')
>>> os.path.splitext(os.path.basename(temp))
('FcuktheCode', '.txt')

ILLUSTRATION

Executed using python3 in Linux terminal

Get the parent directory

>>> os.path.abspath(os.path.join(os.getcwd(), os.pardir))
'/home/legacyboy'

'''
Syntax
>>> os.path.abspath(os.path.join(PATH_TO_GET_THE_PARENT, os.pardir))
'''

If the given path exists

to check if the given path exists

path = '/Home/fcukthecode/file_1'
os.path.exists(path)

#this returns false if path doesn't exist or if the path is a broken symbolic link

ILLUSTRATION

executed using python3

check if the given path is a directory, file, symbolic link, mount point etc

to check if the given path is a directory

#these returns false or true

#to check if the given path is a directory
dirname = '/Home/fcukthecode/python'
os.path.isdir(dirname)

#to check if the given path is a file
filename = dirname + 'main.py'
os.path.isfile(filename)

#to check if the given path is symbolic link
symlink = dirname + 'some_sym_link'
os.path.islink(symlink)

#to check if the given path is a mount point
mount_path = '/Home'
os.path.ismount(mount_path)

ILLUSTRATION

Executed using python3 Linux Terminal

Absolute Path from Relative Path

Use os.path.abspath :

>>> import os
>>> os.getcwd()
'/home/legacyboy/Desktop'
>>> os.path.abspath('fcukthecode')
'/home/legacyboy/Desktop/fcukthecode'
>>> os.path.abspath('../overlord')
'/home/legacyboy/overlord'
>>> os.path.abspath('/overlord')
'/overlord'

ILLUSTRATION

EXECUTED USING PYTHON3

Morae Q!

  1. OS operating system module using path parameter.
  2. Find the smallest and largest integers after sorting using bubble sort.
  3. Find the integer and its number of occurrences.
  4. Algorithm complexity – Big O Notation with Examples.
  5. Linear search.
  6. Map module using series mapping and parallel mapping.
  7. Mapping and Transposing with Map Module.
  8. Map Module/built-in Function.
  9. Linux commands for managing files.
  10. Program which takes two lists and maps two lists into a dictionary.
  11. Splitting strings and substituting using regular expressions regex.
  12. Basics of regular expression Regex.
  13. Find the square value of the dictionary.
  14. Check whether the given key is present in the dictionary.
  15. Matching an expression only in specific places using regular expressions.
  16. Escaping special characters using regular expressions.
  17. Check the key.
  18. Grouping and sub-grouping using regular expressions.
  19. Generate a Dictionary that Contains Numbers in the Form (x , x*x).
  20. Algorithm complexity Big-Theta, Big-Omega and Big-O Notations.