Python Module: os

/Notebook/Notes

Introduction

Python module os handles the operating system. This involves environment variables, files and folders.

This blog only covers a summary of the module methods. See full documentation here.

Summary

import os
Method Args / Kwargs Data Type Description
os.getcwd() returns the current working directory in string
os.chdir() path_name string changes os directory to path_name
os.listdir() returns a list of files and folders
os.mkdir() folder_name string creates a folder named folder_name
os.mkdirs() path_name string creates directory and sub-directories defined by path_name
os.rmdir() folder_name string removes a folder named folder_name
os.removedirs() path_name string removes directory and sub-directories defined by path_name
os.rename() name, new_name string, string renames a file/folder named name into new_name
os.stat() filename string displays the stats/attributes of filename
os.walk() path_name string returns a generator object containing a tuple of (directory in string, sub-directories of directory in list, and filenames of directory in list) for all directories and sub-directories in by path_name
os.environ.get() environment_variable string returns the value of environment_variable. e.g. 'HOME' returns the home directory of the os
os.path.expanduser() ??? ??? ???
os.path.join() base_dir, fname string, string concatenates base directory base_dir to a file/folder fname
os.path.basename() path_name string returns the base file/folder name of the directory path_name
os.path.dirname() path_name string returns the parent directory of the file/folder defined by path_name
os.path.split() path_name string returns a tuple of directory name and file/folder name specified by path_name
os.path.exists() path_name string returns a boolean representing the existence of path_name in the os
os.path.isdir() path_name string checks if the directory path_name is a folder/directory and returns a boolean
os.path.isfile() path_name string checks if the directory path_name is a file and returns a boolean
os.path.splitext() path_name string returns a tuple of root name and extension name of a given directory