OS Module Python Tutorial




In this video, we cover the OS module with Python 3. The main purpose of the OS module is to interact with your operating system. The primary use I find for it is to create folders, remove folders, move folders, and sometimes change the working directory. You can also access the names of files within a file path by doing listdir(). We do not cover that in this video, but that's an option.

The os module is a part of the standard library, or stdlib, within Python 3. This means that it comes with your Python installation, but you still must import it.

Sample code using os:

import os

All of the following code assumes you have os imported. Because it is not a built-in function, you must always import it. It is a part of the standard library, however, so you will not need to download or install it separately from your Python installation.

curDir = os.getcwd()
print(curDir)

The above code will get your current working directory, hence "cwd."

To make a new directory:

os.mkdir('newDir')

To change the name of, or rename, a directory:

os.rename('newDir','newDir2')

To remove a directory:

os.rmdir('newDir2')

With the os module, there are of course many more things we can do. In many scenarios, however, the os module is actually becoming outdated, as there is a superior module to get the job done. We will get to those soon enough. It is still a good idea to at least know some of the basics of the os module. I especially like to use it to create directories. If you ever create a setup.py file, the creation of directories and the placing of files within them will be essential.


There exists 1 challenge(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:






  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial