Python installation os
Installing the OS with Python

Python is a high-level programming language whose powerful features and easy-to-read syntax make it a popular choice among developers and data scientists. Python includes a built-in module called “os” that provides functionality for interacting with the operating system. In this article, we’ll detail how to install Python and use its built-in os module.
Python Installation
First, let’s discuss how to install Python on different operating systems. Python can be installed on Windows, Mac, and Linux. The following steps describe the Python installation process for each of these operating systems.
Windows
Installing Python on Windows is straightforward. Simply follow these steps:
- Visit the official Python website at https://www.python.org/downloads/ and download the latest Python installer.
- Run the downloaded installer, select the “Add Python to PATH” option in the installation wizard, and then click the “Install Now” button.
- After the installation is complete, open a command prompt and enter the following command to verify that Python was installed successfully:
python --version
If the installation is successful, you will see the Python version number.
Mac
Installing Python on a Mac is also very simple. Simply follow these steps:
- Open the Terminal application and enter the following command to install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Python:
brew install python
- After the installation is complete, verify that Python was installed successfully by entering the following command:
python3 --version
Linux
Installing Python on Linux is also simple. Different Linux distributions have different package managers. Here’s how to install Python on some major Linux distributions:
Installing Python on Ubuntu
- Open a terminal and enter the following command to update the package list:
sudo apt update
- Install Python:
sudo apt install python3
- Enter the following command to verify that Python is installed successfully:
python3 --version
Installing Python on CentOS
- Open a terminal and enter the following command to install Python:
sudo yum install python3
- Enter the following command to verify that Python is installed successfully:
python3 --version
Using the os Module
Once Python is successfully installed, we can use its built-in os module to perform file and directory operations. The following are some common examples of operating system module functions:
Get the current working directory
import os
cwd = os.getcwd()
print("Current working directory is:", cwd)
<p>Running result:
<pre><code class="language-python line-numbers">Current working directory is: /Users/username/Documents
<h3>Creating a directory
<pre><code class="language-python line-numbers">import os
os.mkdir("test_dir")
print("Directory created")
Listing files and subdirectories in a directory
import os
files = os.listdir(".")
print("Files and subdirectories in directory are:")
for file in files:
print(file)
Deleting a directory
import os
os.rmdir("test_dir")
print("Directory deleted")
Checking if a file/directory exists
import os
file_path = "example.txt"
if os.path.exists(file_path):
print("File exists")
else:
print("File does not exist")
Renaming a file
import os
os.rename("old_file.txt", "new_file.txt")
print("File renamed successfully")
import os
cwd = os.getcwd()
print("Current working directory is:", cwd)
<p>Running result:
<pre><code class="language-python line-numbers">Current working directory is: /Users/username/Documents
<h3>Creating a directory
<pre><code class="language-python line-numbers">import os
os.mkdir("test_dir")
print("Directory created")
import os
files = os.listdir(".")
print("Files and subdirectories in directory are:")
for file in files:
print(file)
Deleting a directory
import os
os.rmdir("test_dir")
print("Directory deleted")
Checking if a file/directory exists
import os
file_path = "example.txt"
if os.path.exists(file_path):
print("File exists")
else:
print("File does not exist")
Renaming a file
import os
os.rename("old_file.txt", "new_file.txt")
print("File renamed successfully")
These are some common uses of the os module. By using these functions, you can perform various operating system-related operations in Python.
Summary
Through this article, you should now know how to install Python and use its built-in os module to perform operating system-related operations. Python is a powerful programming language with a rich standard library that makes file and directory operations easy for developers.