In [2]:
#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.
In [3]:
import os
In [4]:
#You can check All the options in os module using dir()
#print(dir(os))
In [3]:
#Current working Directory
print(os.getcwd())
/home/kushagra/Desktop/GIT-Python
In [7]:
#Changing Current Working Directory
os.chdir('/home/kushagra/Documents')
In [8]:
print(os.getcwd())
/home/kushagra/Documents
In [9]:
#Listing the files and directories in current Directories
print(os.listdir())
['Software_Testing.docx', 'sushil_kumar_sharma.pdf', 'mysecond.js', 'demofile1.html', 'bzLifecycle.png', 'Assignment', 'Goibibo.pdf', 'Git.txt', 'k8s-mastery', '101_Ratings', 'docker-curriculum', 'Software_Testing.odt', 'SoftwareTesting2.txt', 'SoftwareTesting.txt', 'buildtesting-sanity.txt', 'download.jpeg', 'personal', 'Kubernetes']
In [10]:
#making Directory, makedirs works similar to mkdir -p option
os.mkdir('OS-Demo-1')
os.makedirs('OS-Demo-2/a/b/c')
In [11]:
print(os.listdir())
['Software_Testing.docx', 'sushil_kumar_sharma.pdf', 'mysecond.js', 'demofile1.html', 'bzLifecycle.png', 'Assignment', 'Goibibo.pdf', 'Git.txt', 'k8s-mastery', 'OS-Demo-2', '101_Ratings', 'OS-Demo-1', 'docker-curriculum', 'Software_Testing.odt', 'SoftwareTesting2.txt', 'SoftwareTesting.txt', 'buildtesting-sanity.txt', 'download.jpeg', 'personal', 'Kubernetes']
In [12]:
#Deleting Directories, removedirs will remove sub directories also
os.rmdir('OS-Demo-1')
os.removedirs('OS-Demo-2/a/b/c')
In [13]:
#Rename file or folder
os.rename('bzLifecycle.png','bugzlifecycle.png')
In [14]:
print(os.listdir())
['Software_Testing.docx', 'sushil_kumar_sharma.pdf', 'mysecond.js', 'demofile1.html', 'Assignment', 'Goibibo.pdf', 'Git.txt', 'k8s-mastery', '101_Ratings', 'docker-curriculum', 'Software_Testing.odt', 'SoftwareTesting2.txt', 'SoftwareTesting.txt', 'buildtesting-sanity.txt', 'download.jpeg', 'bugzlifecycle.png', 'personal', 'Kubernetes']
In [16]:
#information about file
print(os.stat("bugzlifecycle.png"))
os.stat_result(st_mode=33204, st_ino=8951510, st_dev=2050, st_nlink=1, st_uid=30000, st_gid=30000, st_size=74246, st_atime=1559019783, st_mtime=1551016824, st_ctime=1561888070)
In [17]:
#Modified time
print(os.stat("bugzlifecycle.png").st_mtime)
1551016824.4557145
In [18]:
#Modified time in Human Readable Format
from datetime import datetime
mod_time=os.stat("bugzlifecycle.png").st_mtime
print(datetime.fromtimestamp(mod_time))
2019-02-24 19:30:24.455714
In [35]:
for dirpath, dirnames, dirfiles in os.walk('/home/kushagra/Desktop/percentile'):
    print("Current dir:", dirpath)
    print("Directories:", dirnames)
    print("Files:", dirfiles)
    print()
Current dir: /home/kushagra/Desktop/percentile
Directories: ['nodeJS']
Files: ['percent_line.py', 'percent_try.py', 'percentile_BT.py', '123', 'new.py', 'yo', 'percent_file.py', 'percentile_interval.py', 'percent_line', 'percentile_aggre.backup.py', 'exp.py', 'percentile_IP.py', '.try.py.swp', '3', 'shell.sh', 'kushagra.tar.gz', 'any_percentile.py~', '2', '1', 'any_percentile.py', 'backup.py', 'percentile_interval.py.backup']

Current dir: /home/kushagra/Desktop/percentile/nodeJS
Directories: []
Files: ['2.py', 'nodeJS_Percentile.py', '1']

In [37]:
#Printing Environment Variable
print(os.environ.get('HOME'))
/home/kushagra
In [38]:
#Joining path and file
file_path=os.path.join(os.environ.get('HOME'),'test.txt')
print(file_path)
/home/kushagra/test.txt
In [39]:
#To get filename
print(os.path.basename('/home/cavisson/test.txt'))
test.txt
In [40]:
#To get path
print(os.path.dirname('/home/cavisson/test.txt'))
/home/cavisson
In [41]:
#To split path and filename
print(os.path.split('/home/cavisson/test.txt'))
('/home/cavisson', 'test.txt')
In [48]:
#To check whether file exists or not
print(os.path.exists("/home/kushagra/test.txt"))
True
In [49]:
print(os.path.exists("/home/cavisson/test.txt"))
False
In [45]:
#Check whether file or not
print(os.path.isfile("/home/kushagra/test.txt"))
True
In [46]:
#Check whether Dir or not
print(os.path.isdir("/home/kushagra"))
True
In [52]:
#Spliting extension
print(os.path.splitext('/tmp/test.txt'))
('/tmp/test', '.txt')
In [53]:
#options with os.path module
print(dir(os.path))
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_get_sep', '_joinrealpath', '_varprog', '_varprogb', 'abspath', 'altsep', 'basename', 'commonpath', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath', 'relpath', 'samefile', 'sameopenfile', 'samestat', 'sep', 'split', 'splitdrive', 'splitext', 'stat', 'supports_unicode_filenames', 'sys']