How to Interact with Operating System using Python and Jupyter Notebook

nilabh Last Updated : 24 Oct, 2024
3 min read

This article was published as a part of the Data Science Blogathon

Introduction

Python is one of the most popular and frequently used languages of recent times, being used for various tasks such as data science, data analytics, web development, machine learning, and automating many tasks. In these processes, many tasks are operating system-dependent. I use Jupyter Notebook to use Python.

While working on a lot of projects, especially when I tried to automate one task or another, I felt such an urge, that only if I can talk to the Operating System directly from my Jupyter Notebook, instead of clicking on the mouse or tapping at my touch-pad frantically whenever I needed to create a new directory/folder, or deleting one. Or even for the simplest of the tasks, to know “Where am I”?

Oh. Don’t you worry, I am not going on a philosophical rant, all I wanted to know is which directory I am working on right now. Also, How to change that directory, without going out of Jupyter.

Accessing the Operating System or in other words, the flexibility to talk to my computer directly, from inside my Jupyter notebook gives me immense freedom to automate my tasks fully.

Without much ado, lets us see what is this magical tool, and why am I so excited about it.

 

Import Miscellaneous operating system interfaces

In [1]:
import os
import platform
 

Small brief about what each one of these Modules does, and where to install them from?

The good news first: You need not install any of these Modules. They come pre-installed with python. So you just open your Jupyter notebook, and import them the way as shown above, and you are good to go.

OS

OS is a short form for Operating systems. OS comes under Python’s standard utility modules. It helps to interact with the OS directly from within the Jupyter Notebook. It makes it possible to perform many operating system tasks automatically. This module in Python has functions for creating a directory, showing its contents, showing the current directory, and also functions to change the current directory, and many more.

platform

Python provides an in-built module platform that fetches system information. platform comes under Python’s standard utility modules.

The platform module retrieves information regarding the system(platform) on which you are currently working (or on which the program is currently being executed). By system info (or platform info) it means the information regarding the device you are using, your OS, version of the OS, node, and more. This module will be particularly useful to check the compatibility of your system with the python version installed on your system or whether the hardware specification of your system meets the requirement of the program or software or applications (or games) you are planning to use.

Note: Throughout this blog, I will be using “Folder” and “Directory” interchangeably. They mean the same, just called by different names in different operating systems.

 

Tell Me about my system

platform.uname()

Let us start with getting out system information using platform. We have already imported the module, and are ready to use it’s functions and methods.

In [2]:
 
platform.uname()
 
Out[2]:
uname_result(system='Windows', node='LP000007031003', release='10', version='10.0.17763', machine='AMD64', processor='AMD64 Family 23 Model 17 Stepping 0, AuthenticAMD')
 

Let us understand the output in brief.

The system is the Operating System Family. Returns the system/OS name, such as ‘Linux’, ‘Darwin’, ‘Java’, ‘Windows’.

Node Returns the computer’s network name (may not be fully qualified!). An empty string is returned if the value cannot be determined.

Release Returns the Operating system’s release, e.g. ‘2.2.0’ or ‘NT’ or 10. Reading it together with the system makes sense. Like here, it’s Windows 10 (System Release).

version Returns the system’s release version, e.g. ‘#3 on degas’. An empty string is returned if the value cannot be determined.

Machine Returns the machine type(or in other words, Processor family), e.g. ‘i386’ or ‘AMD64’. An empty string is returned if the value cannot be determined.

Processor Returns the (real) processor name, e.g. ‘amdk6’. An empty string is returned if the value cannot be determined. Note that many platforms do not provide this information or simply return the same value as for machine().

 

What’s my python Version?

import os
import platform
print(platform.python_version())

Responses From Readers

Clear

Pooja Rai
Pooja Rai

great and successful efforts . thank you

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details