Os module is one the usable module in Python as one can easily access the information of operating system. Like we can create and access folders. If you want to manipulate paths, see the os.path module etc.

Let’s dive into some examples of this module. There are many methods to explore, you can go the documentation as well for more -: https://docs.python.org/3/library/os.html#os.nameIn [1]:

## Importing os
## No need to install, comes under standard library.
import os

In [2]:

## To get the os details on which you are working
## I am on windows so it will return 'nt', If you are on UNIX then it will return POSIX print(os.name)

nt

Accessing the current directory

In [3]:

## To access the current directory in which you are.
print(os.getcwd())

C:\Users\gargr\Documents\mlsite\python\getting started with python

In [4]:

## Listing the files in the current directories.
 print(os.listdir())

['.ipynb_checkpoints', 'built-in functions.ipynb', 'Conditional Statements.ipynb', 'CSV.ipynb', 'Dictionary.ipynb', 'file1.txt', 'file2.txt', 'Files in Python.ipynb', 'Functions.ipynb', 'global and local variables.ipynb', 'Input and Output.ipynb', 'Install and custom modules.ipynb', 'intoduction to python.ipynb', 'list.ipynb', 'loops.ipynb', 'Modules.ipynb', 'my_module.ipynb', 'my_module.py', 'os module.ipynb', 'Regular Expressions.ipynb', 'Statistics in Python.ipynb', 'Strings.ipynb', 'student_details.csv', 'student_details2.csv', 'tuples.ipynb', '__pycache__']

How to make a directory

In [5]:

## First I will change my directory i.e, where i want to create a directory. os.chdir('C:/Users/gargr/Desktop')

## Rechecking
print(os.getcwd())

C:\Users\gargr\Desktop

In [6]:

## It will create a folder/directory on my desktop
os.mkdir('OS module')
## You can check if the folder is created or not.
print(os.listdir())

These are files and folders on my local machine.

['Dev-C++.lnk', 'GitHub Desktop.lnk', 'Grammarly.lnk', 'Icecream recordings', 'Microsoft Edge.lnk', 'OS module',  'trans.ipynb', 'zip', 'Zoom.lnk', 'µTorrent.lnk']

How to remove a directory?

In [7]:

## It will remove the directory
os.rmdir('OS module')

You can see that now the newly created directory(‘OS module’) has been remvoed.

In [8]:

print(os.listdir())

['Dev-C++.lnk', 'GitHub Desktop.lnk', 'Grammarly.lnk', 'Icecream recordings', 'Microsoft Edge.lnk', 'trans.ipynb', 'zip', 'Zoom.lnk', 'µTorrent.lnk']

In [10]:

## Renaming of file can also be done
## Created one os-demo.txt file

os.rename('os-demo.txt','demo.txt')

In [11]:

## Rechecking if file gets renamed or not.
print(os.listdir())

['demo.txt', 'Dev-C++.lnk', 'GitHub Desktop.lnk', 'Grammarly.lnk', 'Icecream recordings', 'Microsoft Edge.lnk', 'trans.ipynb', 'zip', 'Zoom.lnk', 'µTorrent.lnk']

How to traverse the whole directory.

In [12]:

## Check the cwd

print(os.getcwd())

C:\Users\gargr\Desktop

In [13]:

print(os.walk(os.getcwd()))

 

In [14]:

## After executing this you can see it is retiuirning the path, then the folders inside ## the current folder/directory and then the files. ## Similarly it goes on as it found  ## one new folder and it makes a tree like
 
for e in os.walk(os.getcwd()):
     print(e)

('C:\\Users\\gargr\\Desktop', ['cappius_demo_colab', 'codevita', 'Icecream recordings', 'study little things'], ['astro.txt', 'Chapter 23.zip', 'demo.txt', 'desktop.ini', 'Dev-C++.lnk', 'GitHub Desktop.lnk', 'Grammarly.lnk', 'Microsoft Edge.lnk', 'RapidMiner Data.xlsx', 'trans.ipynb', 'zip', 'Zoom.lnk', 'µTorrent.lnk'])
('C:\\Users\\gargr\\Desktop\\cappius_demo_colab', [], ['cnn-3.JPG', 'cnn-4.JPG', 'cnn_1.png', 'cnn_2.png', 'try_keras.py'])
('C:\\Users\\gargr\\Desktop\\codevita', [], ['gcd.py', 'New Doc 2018-08-09 (1).pdf', 'New Doc 2018-08-09 (2).pdf', 'New Doc 2018-08-09 (3).pdf', 'New Doc 2018-08-09 (4).pdf', 'New Doc 2018-08-09 (5).pdf', 'New Doc 2018-08-09.pdf'])
('C:\\Users\\gargr\\Desktop\\Icecream recordings', [], ['bendi.webm', 'frkss.webm', 'ice_audio_20180921-210032.mp3', 'ice_video_20180922-165224.webm', 'ice_video_20180922-173821.webm', 'ice_video_20180922-173900.webm', 'ice_video_20180924-132619.webm', 'ice_video_20180924-133433.webm', 'ice_video_20180924-161444.webm', 'ice_video_20180925-142413.webm', 'My Video.mp4', 'sales_forecasting_1.webm', 'sales_forecasting_part_1.webm', 'sales_forecasting_part_2.webm', 'sales_forecasting_part_3.webm', 'sales_forecasting_part_4.webm', 'sf1.webm', 'sf2.webm', 'sf3.webm', 'sf_final.webm', 'sf_final1.webm', 'sf_final_1.webm', 'sf_final_2.webm', 'sf_final_part1.webm', 'sf_final_part2.webm', 'traknk.mp3', 'trial final.webm', 'trial.webm', 'trial2.webm', 'trial3.webm', 'trial4.mp3', 'trial5.webm', 'trial6.webm', 'trial_p.webm', 'trial_pl.webm'])
('C:\\Users\\gargr\\Desktop\\study little things', [], ['Areviewonimagefeatureextractionandrepresentationtechniques.pdf', 'bi.JPG', 'BoostedTree.pdf', 'cnn_eplain_andrew.png', 'cnn_eplain_andrew2.png', 'cnn_explain_andrew1.png', 'conda cv.png', 'generator in python.PNG', 'linear algebra.pdf', 'linear algebra.pdf~RF90133b6.TMP', 'linkedin.txt', 'ml description.jpeg', 'ML when to use which algo.png', 'object detection - coursera.JPG', 'precisonrecall.png', 'quote.JPG', 'svm overview.jpeg', 'variables info.jpeg'])

Tidy results of above query

In [15]:

for dirpath, folders, files in os.walk(os.getcwd()):
    print('Current path: ',dirpath)
    print('Folders inside the current directory: ',folders)
    print('Files inside current path:',files)
    print('\n')

Current path:  C:\Users\gargr\Desktop
Folders inside the current directory:  ['cappius_demo_colab', 'codevita', 'Icecream recordings', 'study little things']
Files inside current path: ['astro.txt', 'Chapter 23.zip', 'demo.txt', 'desktop.ini', 'Dev-C++.lnk', 'GitHub Desktop.lnk', 'Grammarly.lnk', 'Microsoft Edge.lnk', 'RapidMiner Data.xlsx', 'trans.ipynb', 'zip', 'Zoom.lnk', 'µTorrent.lnk']


Current path:  C:\Users\gargr\Desktop\cappius_demo_colab
Folders inside the current directory:  []
Files inside current path: ['cnn-3.JPG', 'cnn-4.JPG', 'cnn_1.png', 'cnn_2.png', 'try_keras.py']


Current path:  C:\Users\gargr\Desktop\codevita
Folders inside the current directory:  []
Files inside current path: ['gcd.py', 'New Doc 2018-08-09 (1).pdf', 'New Doc 2018-08-09 (2).pdf', 'New Doc 2018-08-09 (3).pdf', 'New Doc 2018-08-09 (4).pdf', 'New Doc 2018-08-09 (5).pdf', 'New Doc 2018-08-09.pdf']


Current path:  C:\Users\gargr\Desktop\Icecream recordings
Folders inside the current directory:  []
Files inside current path: ['bendi.webm', 'frkss.webm', 'ice_audio_20180921-210032.mp3', 'ice_video_20180922-165224.webm', 'ice_video_20180922-173821.webm', 'ice_video_20180922-173900.webm', 'ice_video_20180924-132619.webm', 'ice_video_20180924-133433.webm', 'ice_video_20180924-161444.webm', 'ice_video_20180925-142413.webm', 'My Video.mp4', 'sales_forecasting_1.webm', 'sales_forecasting_part_1.webm', 'sales_forecasting_part_2.webm', 'sales_forecasting_part_3.webm', 'sales_forecasting_part_4.webm', 'sf1.webm', 'sf2.webm', 'sf3.webm', 'sf_final.webm', 'sf_final1.webm', 'sf_final_1.webm', 'sf_final_2.webm', 'sf_final_part1.webm', 'sf_final_part2.webm', 'traknk.mp3', 'trial final.webm', 'trial.webm', 'trial2.webm', 'trial3.webm', 'trial4.mp3', 'trial5.webm', 'trial6.webm', 'trial_p.webm', 'trial_pl.webm']


Current path:  C:\Users\gargr\Desktop\study little things
Folders inside the current directory:  []
Files inside current path: ['Areviewonimagefeatureextractionandrepresentationtechniques.pdf', 'bi.JPG', 'BoostedTree.pdf', 'cnn_eplain_andrew.png', 'cnn_eplain_andrew2.png', 'cnn_explain_andrew1.png', 'conda cv.png', 'generator in python.PNG', 'linear algebra.pdf', 'linear algebra.pdf~RF90133b6.TMP', 'linkedin.txt', 'ml description.jpeg', 'ML when to use which algo.png', 'object detection - coursera.JPG', 'precisonrecall.png', 'quote.JPG', 'svm overview.jpeg', 'variables info.jpeg']


How to manipulate a file inside a directory ?

In [16]:

print(os.getcwd())

C:\Users\gargr\Desktop

In [17]:

## This is how we can access a file inside a directory.
print(os.path.join(os.getcwd() , 'demo.txt'))

C:\Users\gargr\Desktop\demo.txt

There are many more methods in os module such as environment , in os.path itself there are many functionalities. You can explore them at the time of usage or by documentation.