#os Module
#The OS module in python provides functions for interacting with the operating system.
#This module provides a portable way of using operating system dependent functionality.
import os
#You can check All the options in os module using dir()
#print(dir(os))
#Current working Directory
print(os.getcwd())
#Changing Current Working Directory
os.chdir('/home/kushagra/Documents')
print(os.getcwd())
#Listing the files and directories in current Directories
print(os.listdir())
#making Directory, makedirs works similar to mkdir -p option
os.mkdir('OS-Demo-1')
os.makedirs('OS-Demo-2/a/b/c')
print(os.listdir())
#Deleting Directories, removedirs will remove sub directories also
os.rmdir('OS-Demo-1')
os.removedirs('OS-Demo-2/a/b/c')
#Rename file or folder
os.rename('bzLifecycle.png','bugzlifecycle.png')
print(os.listdir())
#information about file
print(os.stat("bugzlifecycle.png"))
#Modified time
print(os.stat("bugzlifecycle.png").st_mtime)
#Modified time in Human Readable Format
from datetime import datetime
mod_time=os.stat("bugzlifecycle.png").st_mtime
print(datetime.fromtimestamp(mod_time))
for dirpath, dirnames, dirfiles in os.walk('/home/kushagra/Desktop/percentile'):
print("Current dir:", dirpath)
print("Directories:", dirnames)
print("Files:", dirfiles)
print()
#Printing Environment Variable
print(os.environ.get('HOME'))
#Joining path and file
file_path=os.path.join(os.environ.get('HOME'),'test.txt')
print(file_path)
#To get filename
print(os.path.basename('/home/cavisson/test.txt'))
#To get path
print(os.path.dirname('/home/cavisson/test.txt'))
#To split path and filename
print(os.path.split('/home/cavisson/test.txt'))
#To check whether file exists or not
print(os.path.exists("/home/kushagra/test.txt"))
print(os.path.exists("/home/cavisson/test.txt"))
#Check whether file or not
print(os.path.isfile("/home/kushagra/test.txt"))
#Check whether Dir or not
print(os.path.isdir("/home/kushagra"))
#Spliting extension
print(os.path.splitext('/tmp/test.txt'))
#options with os.path module
print(dir(os.path))